transaction.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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, int mark)
  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. mark);
  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, int mark)
  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, start, &start, &end,
  382. mark);
  383. if (ret)
  384. break;
  385. clear_extent_bits(dirty_pages, start, end, mark, 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, int mark)
  415. {
  416. int ret;
  417. int ret2;
  418. ret = btrfs_write_marked_extents(root, dirty_pages, mark);
  419. ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
  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. EXTENT_DIRTY);
  433. }
  434. /*
  435. * this is used to update the root pointer in the tree of tree roots.
  436. *
  437. * But, in the case of the extent allocation tree, updating the root
  438. * pointer may allocate blocks which may change the root of the extent
  439. * allocation tree.
  440. *
  441. * So, this loops and repeats and makes sure the cowonly root didn't
  442. * change while the root pointer was being updated in the metadata.
  443. */
  444. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  445. struct btrfs_root *root)
  446. {
  447. int ret;
  448. u64 old_root_bytenr;
  449. struct btrfs_root *tree_root = root->fs_info->tree_root;
  450. btrfs_write_dirty_block_groups(trans, root);
  451. while (1) {
  452. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  453. if (old_root_bytenr == root->node->start)
  454. break;
  455. btrfs_set_root_node(&root->root_item, root->node);
  456. ret = btrfs_update_root(trans, tree_root,
  457. &root->root_key,
  458. &root->root_item);
  459. BUG_ON(ret);
  460. ret = btrfs_write_dirty_block_groups(trans, root);
  461. BUG_ON(ret);
  462. }
  463. if (root != root->fs_info->extent_root)
  464. switch_commit_root(root);
  465. return 0;
  466. }
  467. /*
  468. * update all the cowonly tree roots on disk
  469. */
  470. static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
  471. struct btrfs_root *root)
  472. {
  473. struct btrfs_fs_info *fs_info = root->fs_info;
  474. struct list_head *next;
  475. struct extent_buffer *eb;
  476. int ret;
  477. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  478. BUG_ON(ret);
  479. eb = btrfs_lock_root_node(fs_info->tree_root);
  480. btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
  481. btrfs_tree_unlock(eb);
  482. free_extent_buffer(eb);
  483. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  484. BUG_ON(ret);
  485. while (!list_empty(&fs_info->dirty_cowonly_roots)) {
  486. next = fs_info->dirty_cowonly_roots.next;
  487. list_del_init(next);
  488. root = list_entry(next, struct btrfs_root, dirty_list);
  489. update_cowonly_root(trans, root);
  490. }
  491. down_write(&fs_info->extent_commit_sem);
  492. switch_commit_root(fs_info->extent_root);
  493. up_write(&fs_info->extent_commit_sem);
  494. return 0;
  495. }
  496. /*
  497. * dead roots are old snapshots that need to be deleted. This allocates
  498. * a dirty root struct and adds it into the list of dead roots that need to
  499. * be deleted
  500. */
  501. int btrfs_add_dead_root(struct btrfs_root *root)
  502. {
  503. mutex_lock(&root->fs_info->trans_mutex);
  504. list_add(&root->root_list, &root->fs_info->dead_roots);
  505. mutex_unlock(&root->fs_info->trans_mutex);
  506. return 0;
  507. }
  508. /*
  509. * update all the cowonly tree roots on disk
  510. */
  511. static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
  512. struct btrfs_root *root)
  513. {
  514. struct btrfs_root *gang[8];
  515. struct btrfs_fs_info *fs_info = root->fs_info;
  516. int i;
  517. int ret;
  518. int err = 0;
  519. while (1) {
  520. ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
  521. (void **)gang, 0,
  522. ARRAY_SIZE(gang),
  523. BTRFS_ROOT_TRANS_TAG);
  524. if (ret == 0)
  525. break;
  526. for (i = 0; i < ret; i++) {
  527. root = gang[i];
  528. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  529. (unsigned long)root->root_key.objectid,
  530. BTRFS_ROOT_TRANS_TAG);
  531. btrfs_free_log(trans, root);
  532. btrfs_update_reloc_root(trans, root);
  533. if (root->commit_root != root->node) {
  534. switch_commit_root(root);
  535. btrfs_set_root_node(&root->root_item,
  536. root->node);
  537. }
  538. err = btrfs_update_root(trans, fs_info->tree_root,
  539. &root->root_key,
  540. &root->root_item);
  541. if (err)
  542. break;
  543. }
  544. }
  545. return err;
  546. }
  547. /*
  548. * defrag a given btree. If cacheonly == 1, this won't read from the disk,
  549. * otherwise every leaf in the btree is read and defragged.
  550. */
  551. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  552. {
  553. struct btrfs_fs_info *info = root->fs_info;
  554. int ret;
  555. struct btrfs_trans_handle *trans;
  556. unsigned long nr;
  557. smp_mb();
  558. if (root->defrag_running)
  559. return 0;
  560. trans = btrfs_start_transaction(root, 1);
  561. while (1) {
  562. root->defrag_running = 1;
  563. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  564. nr = trans->blocks_used;
  565. btrfs_end_transaction(trans, root);
  566. btrfs_btree_balance_dirty(info->tree_root, nr);
  567. cond_resched();
  568. trans = btrfs_start_transaction(root, 1);
  569. if (root->fs_info->closing || ret != -EAGAIN)
  570. break;
  571. }
  572. root->defrag_running = 0;
  573. smp_mb();
  574. btrfs_end_transaction(trans, root);
  575. return 0;
  576. }
  577. #if 0
  578. /*
  579. * when dropping snapshots, we generate a ton of delayed refs, and it makes
  580. * sense not to join the transaction while it is trying to flush the current
  581. * queue of delayed refs out.
  582. *
  583. * This is used by the drop snapshot code only
  584. */
  585. static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
  586. {
  587. DEFINE_WAIT(wait);
  588. mutex_lock(&info->trans_mutex);
  589. while (info->running_transaction &&
  590. info->running_transaction->delayed_refs.flushing) {
  591. prepare_to_wait(&info->transaction_wait, &wait,
  592. TASK_UNINTERRUPTIBLE);
  593. mutex_unlock(&info->trans_mutex);
  594. schedule();
  595. mutex_lock(&info->trans_mutex);
  596. finish_wait(&info->transaction_wait, &wait);
  597. }
  598. mutex_unlock(&info->trans_mutex);
  599. return 0;
  600. }
  601. /*
  602. * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
  603. * all of them
  604. */
  605. int btrfs_drop_dead_root(struct btrfs_root *root)
  606. {
  607. struct btrfs_trans_handle *trans;
  608. struct btrfs_root *tree_root = root->fs_info->tree_root;
  609. unsigned long nr;
  610. int ret;
  611. while (1) {
  612. /*
  613. * we don't want to jump in and create a bunch of
  614. * delayed refs if the transaction is starting to close
  615. */
  616. wait_transaction_pre_flush(tree_root->fs_info);
  617. trans = btrfs_start_transaction(tree_root, 1);
  618. /*
  619. * we've joined a transaction, make sure it isn't
  620. * closing right now
  621. */
  622. if (trans->transaction->delayed_refs.flushing) {
  623. btrfs_end_transaction(trans, tree_root);
  624. continue;
  625. }
  626. ret = btrfs_drop_snapshot(trans, root);
  627. if (ret != -EAGAIN)
  628. break;
  629. ret = btrfs_update_root(trans, tree_root,
  630. &root->root_key,
  631. &root->root_item);
  632. if (ret)
  633. break;
  634. nr = trans->blocks_used;
  635. ret = btrfs_end_transaction(trans, tree_root);
  636. BUG_ON(ret);
  637. btrfs_btree_balance_dirty(tree_root, nr);
  638. cond_resched();
  639. }
  640. BUG_ON(ret);
  641. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  642. BUG_ON(ret);
  643. nr = trans->blocks_used;
  644. ret = btrfs_end_transaction(trans, tree_root);
  645. BUG_ON(ret);
  646. free_extent_buffer(root->node);
  647. free_extent_buffer(root->commit_root);
  648. kfree(root);
  649. btrfs_btree_balance_dirty(tree_root, nr);
  650. return ret;
  651. }
  652. #endif
  653. /*
  654. * new snapshots need to be created at a very specific time in the
  655. * transaction commit. This does the actual creation
  656. */
  657. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  658. struct btrfs_fs_info *fs_info,
  659. struct btrfs_pending_snapshot *pending)
  660. {
  661. struct btrfs_key key;
  662. struct btrfs_root_item *new_root_item;
  663. struct btrfs_root *tree_root = fs_info->tree_root;
  664. struct btrfs_root *root = pending->root;
  665. struct extent_buffer *tmp;
  666. struct extent_buffer *old;
  667. int ret;
  668. u64 objectid;
  669. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  670. if (!new_root_item) {
  671. ret = -ENOMEM;
  672. goto fail;
  673. }
  674. ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
  675. if (ret)
  676. goto fail;
  677. record_root_in_trans(trans, root);
  678. btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  679. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  680. key.objectid = objectid;
  681. /* record when the snapshot was created in key.offset */
  682. key.offset = trans->transid;
  683. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  684. old = btrfs_lock_root_node(root);
  685. btrfs_cow_block(trans, root, old, NULL, 0, &old);
  686. btrfs_set_lock_blocking(old);
  687. btrfs_copy_root(trans, root, old, &tmp, objectid);
  688. btrfs_tree_unlock(old);
  689. free_extent_buffer(old);
  690. btrfs_set_root_node(new_root_item, tmp);
  691. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  692. new_root_item);
  693. btrfs_tree_unlock(tmp);
  694. free_extent_buffer(tmp);
  695. if (ret)
  696. goto fail;
  697. key.offset = (u64)-1;
  698. memcpy(&pending->root_key, &key, sizeof(key));
  699. fail:
  700. kfree(new_root_item);
  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 btrfs_root *parent_root;
  712. parent_inode = pending->dentry->d_parent->d_inode;
  713. parent_root = BTRFS_I(parent_inode)->root;
  714. trans = btrfs_join_transaction(parent_root, 1);
  715. /*
  716. * insert the directory item
  717. */
  718. namelen = strlen(pending->name);
  719. ret = btrfs_set_inode_index(parent_inode, &index);
  720. ret = btrfs_insert_dir_item(trans, parent_root,
  721. pending->name, namelen,
  722. parent_inode->i_ino,
  723. &pending->root_key, BTRFS_FT_DIR, index);
  724. if (ret)
  725. goto fail;
  726. btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2);
  727. ret = btrfs_update_inode(trans, parent_root, parent_inode);
  728. BUG_ON(ret);
  729. ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
  730. pending->root_key.objectid,
  731. parent_root->root_key.objectid,
  732. parent_inode->i_ino, index, pending->name,
  733. namelen);
  734. BUG_ON(ret);
  735. fail:
  736. btrfs_end_transaction(trans, fs_info->fs_root);
  737. return ret;
  738. }
  739. /*
  740. * create all the snapshots we've scheduled for creation
  741. */
  742. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  743. struct btrfs_fs_info *fs_info)
  744. {
  745. struct btrfs_pending_snapshot *pending;
  746. struct list_head *head = &trans->transaction->pending_snapshots;
  747. int ret;
  748. list_for_each_entry(pending, head, list) {
  749. ret = create_pending_snapshot(trans, fs_info, pending);
  750. BUG_ON(ret);
  751. }
  752. return 0;
  753. }
  754. static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans,
  755. struct btrfs_fs_info *fs_info)
  756. {
  757. struct btrfs_pending_snapshot *pending;
  758. struct list_head *head = &trans->transaction->pending_snapshots;
  759. int ret;
  760. while (!list_empty(head)) {
  761. pending = list_entry(head->next,
  762. struct btrfs_pending_snapshot, list);
  763. ret = finish_pending_snapshot(fs_info, pending);
  764. BUG_ON(ret);
  765. list_del(&pending->list);
  766. kfree(pending->name);
  767. kfree(pending);
  768. }
  769. return 0;
  770. }
  771. static void update_super_roots(struct btrfs_root *root)
  772. {
  773. struct btrfs_root_item *root_item;
  774. struct btrfs_super_block *super;
  775. super = &root->fs_info->super_copy;
  776. root_item = &root->fs_info->chunk_root->root_item;
  777. super->chunk_root = root_item->bytenr;
  778. super->chunk_root_generation = root_item->generation;
  779. super->chunk_root_level = root_item->level;
  780. root_item = &root->fs_info->tree_root->root_item;
  781. super->root = root_item->bytenr;
  782. super->generation = root_item->generation;
  783. super->root_level = root_item->level;
  784. }
  785. int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
  786. {
  787. int ret = 0;
  788. spin_lock(&info->new_trans_lock);
  789. if (info->running_transaction)
  790. ret = info->running_transaction->in_commit;
  791. spin_unlock(&info->new_trans_lock);
  792. return ret;
  793. }
  794. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  795. struct btrfs_root *root)
  796. {
  797. unsigned long joined = 0;
  798. unsigned long timeout = 1;
  799. struct btrfs_transaction *cur_trans;
  800. struct btrfs_transaction *prev_trans = NULL;
  801. DEFINE_WAIT(wait);
  802. int ret;
  803. int should_grow = 0;
  804. unsigned long now = get_seconds();
  805. int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
  806. btrfs_run_ordered_operations(root, 0);
  807. /* make a pass through all the delayed refs we have so far
  808. * any runnings procs may add more while we are here
  809. */
  810. ret = btrfs_run_delayed_refs(trans, root, 0);
  811. BUG_ON(ret);
  812. cur_trans = trans->transaction;
  813. /*
  814. * set the flushing flag so procs in this transaction have to
  815. * start sending their work down.
  816. */
  817. cur_trans->delayed_refs.flushing = 1;
  818. ret = btrfs_run_delayed_refs(trans, root, 0);
  819. BUG_ON(ret);
  820. mutex_lock(&root->fs_info->trans_mutex);
  821. if (cur_trans->in_commit) {
  822. cur_trans->use_count++;
  823. mutex_unlock(&root->fs_info->trans_mutex);
  824. btrfs_end_transaction(trans, root);
  825. ret = wait_for_commit(root, cur_trans);
  826. BUG_ON(ret);
  827. mutex_lock(&root->fs_info->trans_mutex);
  828. put_transaction(cur_trans);
  829. mutex_unlock(&root->fs_info->trans_mutex);
  830. return 0;
  831. }
  832. trans->transaction->in_commit = 1;
  833. trans->transaction->blocked = 1;
  834. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  835. prev_trans = list_entry(cur_trans->list.prev,
  836. struct btrfs_transaction, list);
  837. if (!prev_trans->commit_done) {
  838. prev_trans->use_count++;
  839. mutex_unlock(&root->fs_info->trans_mutex);
  840. wait_for_commit(root, prev_trans);
  841. mutex_lock(&root->fs_info->trans_mutex);
  842. put_transaction(prev_trans);
  843. }
  844. }
  845. if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
  846. should_grow = 1;
  847. do {
  848. int snap_pending = 0;
  849. joined = cur_trans->num_joined;
  850. if (!list_empty(&trans->transaction->pending_snapshots))
  851. snap_pending = 1;
  852. WARN_ON(cur_trans != trans->transaction);
  853. prepare_to_wait(&cur_trans->writer_wait, &wait,
  854. TASK_UNINTERRUPTIBLE);
  855. if (cur_trans->num_writers > 1)
  856. timeout = MAX_SCHEDULE_TIMEOUT;
  857. else if (should_grow)
  858. timeout = 1;
  859. mutex_unlock(&root->fs_info->trans_mutex);
  860. if (flush_on_commit) {
  861. btrfs_start_delalloc_inodes(root);
  862. ret = btrfs_wait_ordered_extents(root, 0);
  863. BUG_ON(ret);
  864. } else if (snap_pending) {
  865. ret = btrfs_wait_ordered_extents(root, 1);
  866. BUG_ON(ret);
  867. }
  868. /*
  869. * rename don't use btrfs_join_transaction, so, once we
  870. * set the transaction to blocked above, we aren't going
  871. * to get any new ordered operations. We can safely run
  872. * it here and no for sure that nothing new will be added
  873. * to the list
  874. */
  875. btrfs_run_ordered_operations(root, 1);
  876. smp_mb();
  877. if (cur_trans->num_writers > 1 || should_grow)
  878. schedule_timeout(timeout);
  879. mutex_lock(&root->fs_info->trans_mutex);
  880. finish_wait(&cur_trans->writer_wait, &wait);
  881. } while (cur_trans->num_writers > 1 ||
  882. (should_grow && cur_trans->num_joined != joined));
  883. ret = create_pending_snapshots(trans, root->fs_info);
  884. BUG_ON(ret);
  885. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  886. BUG_ON(ret);
  887. WARN_ON(cur_trans != trans->transaction);
  888. /* btrfs_commit_tree_roots is responsible for getting the
  889. * various roots consistent with each other. Every pointer
  890. * in the tree of tree roots has to point to the most up to date
  891. * root for every subvolume and other tree. So, we have to keep
  892. * the tree logging code from jumping in and changing any
  893. * of the trees.
  894. *
  895. * At this point in the commit, there can't be any tree-log
  896. * writers, but a little lower down we drop the trans mutex
  897. * and let new people in. By holding the tree_log_mutex
  898. * from now until after the super is written, we avoid races
  899. * with the tree-log code.
  900. */
  901. mutex_lock(&root->fs_info->tree_log_mutex);
  902. ret = commit_fs_roots(trans, root);
  903. BUG_ON(ret);
  904. /* commit_fs_roots gets rid of all the tree log roots, it is now
  905. * safe to free the root of tree log roots
  906. */
  907. btrfs_free_log_root_tree(trans, root->fs_info);
  908. ret = commit_cowonly_roots(trans, root);
  909. BUG_ON(ret);
  910. btrfs_prepare_extent_commit(trans, root);
  911. cur_trans = root->fs_info->running_transaction;
  912. spin_lock(&root->fs_info->new_trans_lock);
  913. root->fs_info->running_transaction = NULL;
  914. spin_unlock(&root->fs_info->new_trans_lock);
  915. btrfs_set_root_node(&root->fs_info->tree_root->root_item,
  916. root->fs_info->tree_root->node);
  917. switch_commit_root(root->fs_info->tree_root);
  918. btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
  919. root->fs_info->chunk_root->node);
  920. switch_commit_root(root->fs_info->chunk_root);
  921. update_super_roots(root);
  922. if (!root->fs_info->log_root_recovering) {
  923. btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
  924. btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
  925. }
  926. memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
  927. sizeof(root->fs_info->super_copy));
  928. trans->transaction->blocked = 0;
  929. wake_up(&root->fs_info->transaction_wait);
  930. mutex_unlock(&root->fs_info->trans_mutex);
  931. ret = btrfs_write_and_wait_transaction(trans, root);
  932. BUG_ON(ret);
  933. write_ctree_super(trans, root, 0);
  934. /*
  935. * the super is written, we can safely allow the tree-loggers
  936. * to go about their business
  937. */
  938. mutex_unlock(&root->fs_info->tree_log_mutex);
  939. btrfs_finish_extent_commit(trans, root);
  940. /* do the directory inserts of any pending snapshot creations */
  941. finish_pending_snapshots(trans, root->fs_info);
  942. mutex_lock(&root->fs_info->trans_mutex);
  943. cur_trans->commit_done = 1;
  944. root->fs_info->last_trans_committed = cur_trans->transid;
  945. wake_up(&cur_trans->commit_wait);
  946. put_transaction(cur_trans);
  947. put_transaction(cur_trans);
  948. mutex_unlock(&root->fs_info->trans_mutex);
  949. if (current->journal_info == trans)
  950. current->journal_info = NULL;
  951. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  952. return ret;
  953. }
  954. /*
  955. * interface function to delete all the snapshots we have scheduled for deletion
  956. */
  957. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  958. {
  959. LIST_HEAD(list);
  960. struct btrfs_fs_info *fs_info = root->fs_info;
  961. mutex_lock(&fs_info->trans_mutex);
  962. list_splice_init(&fs_info->dead_roots, &list);
  963. mutex_unlock(&fs_info->trans_mutex);
  964. while (!list_empty(&list)) {
  965. root = list_entry(list.next, struct btrfs_root, root_list);
  966. list_del(&root->root_list);
  967. if (btrfs_header_backref_rev(root->node) <
  968. BTRFS_MIXED_BACKREF_REV)
  969. btrfs_drop_snapshot(root, 0);
  970. else
  971. btrfs_drop_snapshot(root, 1);
  972. }
  973. return 0;
  974. }