transaction.c 31 KB

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