transaction.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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. btrfs_orphan_commit_root(trans, root);
  596. if (root->commit_root != root->node) {
  597. switch_commit_root(root);
  598. btrfs_set_root_node(&root->root_item,
  599. root->node);
  600. }
  601. err = btrfs_update_root(trans, fs_info->tree_root,
  602. &root->root_key,
  603. &root->root_item);
  604. if (err)
  605. break;
  606. }
  607. }
  608. return err;
  609. }
  610. /*
  611. * defrag a given btree. If cacheonly == 1, this won't read from the disk,
  612. * otherwise every leaf in the btree is read and defragged.
  613. */
  614. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  615. {
  616. struct btrfs_fs_info *info = root->fs_info;
  617. struct btrfs_trans_handle *trans;
  618. int ret;
  619. unsigned long nr;
  620. if (xchg(&root->defrag_running, 1))
  621. return 0;
  622. while (1) {
  623. trans = btrfs_start_transaction(root, 0);
  624. if (IS_ERR(trans))
  625. return PTR_ERR(trans);
  626. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  627. nr = trans->blocks_used;
  628. btrfs_end_transaction(trans, root);
  629. btrfs_btree_balance_dirty(info->tree_root, nr);
  630. cond_resched();
  631. if (root->fs_info->closing || ret != -EAGAIN)
  632. break;
  633. }
  634. root->defrag_running = 0;
  635. return ret;
  636. }
  637. #if 0
  638. /*
  639. * when dropping snapshots, we generate a ton of delayed refs, and it makes
  640. * sense not to join the transaction while it is trying to flush the current
  641. * queue of delayed refs out.
  642. *
  643. * This is used by the drop snapshot code only
  644. */
  645. static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
  646. {
  647. DEFINE_WAIT(wait);
  648. mutex_lock(&info->trans_mutex);
  649. while (info->running_transaction &&
  650. info->running_transaction->delayed_refs.flushing) {
  651. prepare_to_wait(&info->transaction_wait, &wait,
  652. TASK_UNINTERRUPTIBLE);
  653. mutex_unlock(&info->trans_mutex);
  654. schedule();
  655. mutex_lock(&info->trans_mutex);
  656. finish_wait(&info->transaction_wait, &wait);
  657. }
  658. mutex_unlock(&info->trans_mutex);
  659. return 0;
  660. }
  661. /*
  662. * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
  663. * all of them
  664. */
  665. int btrfs_drop_dead_root(struct btrfs_root *root)
  666. {
  667. struct btrfs_trans_handle *trans;
  668. struct btrfs_root *tree_root = root->fs_info->tree_root;
  669. unsigned long nr;
  670. int ret;
  671. while (1) {
  672. /*
  673. * we don't want to jump in and create a bunch of
  674. * delayed refs if the transaction is starting to close
  675. */
  676. wait_transaction_pre_flush(tree_root->fs_info);
  677. trans = btrfs_start_transaction(tree_root, 1);
  678. /*
  679. * we've joined a transaction, make sure it isn't
  680. * closing right now
  681. */
  682. if (trans->transaction->delayed_refs.flushing) {
  683. btrfs_end_transaction(trans, tree_root);
  684. continue;
  685. }
  686. ret = btrfs_drop_snapshot(trans, root);
  687. if (ret != -EAGAIN)
  688. break;
  689. ret = btrfs_update_root(trans, tree_root,
  690. &root->root_key,
  691. &root->root_item);
  692. if (ret)
  693. break;
  694. nr = trans->blocks_used;
  695. ret = btrfs_end_transaction(trans, tree_root);
  696. BUG_ON(ret);
  697. btrfs_btree_balance_dirty(tree_root, nr);
  698. cond_resched();
  699. }
  700. BUG_ON(ret);
  701. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  702. BUG_ON(ret);
  703. nr = trans->blocks_used;
  704. ret = btrfs_end_transaction(trans, tree_root);
  705. BUG_ON(ret);
  706. free_extent_buffer(root->node);
  707. free_extent_buffer(root->commit_root);
  708. kfree(root);
  709. btrfs_btree_balance_dirty(tree_root, nr);
  710. return ret;
  711. }
  712. #endif
  713. /*
  714. * new snapshots need to be created at a very specific time in the
  715. * transaction commit. This does the actual creation
  716. */
  717. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  718. struct btrfs_fs_info *fs_info,
  719. struct btrfs_pending_snapshot *pending)
  720. {
  721. struct btrfs_key key;
  722. struct btrfs_root_item *new_root_item;
  723. struct btrfs_root *tree_root = fs_info->tree_root;
  724. struct btrfs_root *root = pending->root;
  725. struct btrfs_root *parent_root;
  726. struct inode *parent_inode;
  727. struct dentry *dentry;
  728. struct extent_buffer *tmp;
  729. struct extent_buffer *old;
  730. int ret;
  731. int retries = 0;
  732. u64 to_reserve = 0;
  733. u64 index = 0;
  734. u64 objectid;
  735. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  736. if (!new_root_item) {
  737. pending->error = -ENOMEM;
  738. goto fail;
  739. }
  740. ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
  741. if (ret) {
  742. pending->error = ret;
  743. goto fail;
  744. }
  745. btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
  746. btrfs_orphan_pre_snapshot(trans, pending, &to_reserve);
  747. if (to_reserve > 0) {
  748. ret = btrfs_block_rsv_add(trans, root, &pending->block_rsv,
  749. to_reserve, &retries);
  750. if (ret) {
  751. pending->error = ret;
  752. goto fail;
  753. }
  754. }
  755. key.objectid = objectid;
  756. key.offset = (u64)-1;
  757. key.type = BTRFS_ROOT_ITEM_KEY;
  758. trans->block_rsv = &pending->block_rsv;
  759. dentry = pending->dentry;
  760. parent_inode = dentry->d_parent->d_inode;
  761. parent_root = BTRFS_I(parent_inode)->root;
  762. record_root_in_trans(trans, parent_root);
  763. /*
  764. * insert the directory item
  765. */
  766. ret = btrfs_set_inode_index(parent_inode, &index);
  767. BUG_ON(ret);
  768. ret = btrfs_insert_dir_item(trans, parent_root,
  769. dentry->d_name.name, dentry->d_name.len,
  770. parent_inode->i_ino, &key,
  771. BTRFS_FT_DIR, index);
  772. BUG_ON(ret);
  773. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  774. dentry->d_name.len * 2);
  775. ret = btrfs_update_inode(trans, parent_root, parent_inode);
  776. BUG_ON(ret);
  777. record_root_in_trans(trans, root);
  778. btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  779. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  780. old = btrfs_lock_root_node(root);
  781. btrfs_cow_block(trans, root, old, NULL, 0, &old);
  782. btrfs_set_lock_blocking(old);
  783. btrfs_copy_root(trans, root, old, &tmp, objectid);
  784. btrfs_tree_unlock(old);
  785. free_extent_buffer(old);
  786. btrfs_set_root_node(new_root_item, tmp);
  787. /* record when the snapshot was created in key.offset */
  788. key.offset = trans->transid;
  789. ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
  790. btrfs_tree_unlock(tmp);
  791. free_extent_buffer(tmp);
  792. BUG_ON(ret);
  793. /*
  794. * insert root back/forward references
  795. */
  796. ret = btrfs_add_root_ref(trans, tree_root, objectid,
  797. parent_root->root_key.objectid,
  798. parent_inode->i_ino, index,
  799. dentry->d_name.name, dentry->d_name.len);
  800. BUG_ON(ret);
  801. key.offset = (u64)-1;
  802. pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
  803. BUG_ON(IS_ERR(pending->snap));
  804. btrfs_reloc_post_snapshot(trans, pending);
  805. btrfs_orphan_post_snapshot(trans, pending);
  806. fail:
  807. kfree(new_root_item);
  808. btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
  809. return 0;
  810. }
  811. /*
  812. * create all the snapshots we've scheduled for creation
  813. */
  814. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  815. struct btrfs_fs_info *fs_info)
  816. {
  817. struct btrfs_pending_snapshot *pending;
  818. struct list_head *head = &trans->transaction->pending_snapshots;
  819. int ret;
  820. list_for_each_entry(pending, head, list) {
  821. ret = create_pending_snapshot(trans, fs_info, pending);
  822. BUG_ON(ret);
  823. }
  824. return 0;
  825. }
  826. static void update_super_roots(struct btrfs_root *root)
  827. {
  828. struct btrfs_root_item *root_item;
  829. struct btrfs_super_block *super;
  830. super = &root->fs_info->super_copy;
  831. root_item = &root->fs_info->chunk_root->root_item;
  832. super->chunk_root = root_item->bytenr;
  833. super->chunk_root_generation = root_item->generation;
  834. super->chunk_root_level = root_item->level;
  835. root_item = &root->fs_info->tree_root->root_item;
  836. super->root = root_item->bytenr;
  837. super->generation = root_item->generation;
  838. super->root_level = root_item->level;
  839. }
  840. int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
  841. {
  842. int ret = 0;
  843. spin_lock(&info->new_trans_lock);
  844. if (info->running_transaction)
  845. ret = info->running_transaction->in_commit;
  846. spin_unlock(&info->new_trans_lock);
  847. return ret;
  848. }
  849. int btrfs_transaction_blocked(struct btrfs_fs_info *info)
  850. {
  851. int ret = 0;
  852. spin_lock(&info->new_trans_lock);
  853. if (info->running_transaction)
  854. ret = info->running_transaction->blocked;
  855. spin_unlock(&info->new_trans_lock);
  856. return ret;
  857. }
  858. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  859. struct btrfs_root *root)
  860. {
  861. unsigned long joined = 0;
  862. unsigned long timeout = 1;
  863. struct btrfs_transaction *cur_trans;
  864. struct btrfs_transaction *prev_trans = NULL;
  865. DEFINE_WAIT(wait);
  866. int ret;
  867. int should_grow = 0;
  868. unsigned long now = get_seconds();
  869. int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
  870. btrfs_run_ordered_operations(root, 0);
  871. /* make a pass through all the delayed refs we have so far
  872. * any runnings procs may add more while we are here
  873. */
  874. ret = btrfs_run_delayed_refs(trans, root, 0);
  875. BUG_ON(ret);
  876. btrfs_trans_release_metadata(trans, root);
  877. cur_trans = trans->transaction;
  878. /*
  879. * set the flushing flag so procs in this transaction have to
  880. * start sending their work down.
  881. */
  882. cur_trans->delayed_refs.flushing = 1;
  883. ret = btrfs_run_delayed_refs(trans, root, 0);
  884. BUG_ON(ret);
  885. mutex_lock(&root->fs_info->trans_mutex);
  886. if (cur_trans->in_commit) {
  887. cur_trans->use_count++;
  888. mutex_unlock(&root->fs_info->trans_mutex);
  889. btrfs_end_transaction(trans, root);
  890. ret = wait_for_commit(root, cur_trans);
  891. BUG_ON(ret);
  892. mutex_lock(&root->fs_info->trans_mutex);
  893. put_transaction(cur_trans);
  894. mutex_unlock(&root->fs_info->trans_mutex);
  895. return 0;
  896. }
  897. trans->transaction->in_commit = 1;
  898. trans->transaction->blocked = 1;
  899. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  900. prev_trans = list_entry(cur_trans->list.prev,
  901. struct btrfs_transaction, list);
  902. if (!prev_trans->commit_done) {
  903. prev_trans->use_count++;
  904. mutex_unlock(&root->fs_info->trans_mutex);
  905. wait_for_commit(root, prev_trans);
  906. mutex_lock(&root->fs_info->trans_mutex);
  907. put_transaction(prev_trans);
  908. }
  909. }
  910. if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
  911. should_grow = 1;
  912. do {
  913. int snap_pending = 0;
  914. joined = cur_trans->num_joined;
  915. if (!list_empty(&trans->transaction->pending_snapshots))
  916. snap_pending = 1;
  917. WARN_ON(cur_trans != trans->transaction);
  918. if (cur_trans->num_writers > 1)
  919. timeout = MAX_SCHEDULE_TIMEOUT;
  920. else if (should_grow)
  921. timeout = 1;
  922. mutex_unlock(&root->fs_info->trans_mutex);
  923. if (flush_on_commit || snap_pending) {
  924. btrfs_start_delalloc_inodes(root, 1);
  925. ret = btrfs_wait_ordered_extents(root, 0, 1);
  926. BUG_ON(ret);
  927. }
  928. /*
  929. * rename don't use btrfs_join_transaction, so, once we
  930. * set the transaction to blocked above, we aren't going
  931. * to get any new ordered operations. We can safely run
  932. * it here and no for sure that nothing new will be added
  933. * to the list
  934. */
  935. btrfs_run_ordered_operations(root, 1);
  936. prepare_to_wait(&cur_trans->writer_wait, &wait,
  937. TASK_UNINTERRUPTIBLE);
  938. smp_mb();
  939. if (cur_trans->num_writers > 1 || should_grow)
  940. schedule_timeout(timeout);
  941. mutex_lock(&root->fs_info->trans_mutex);
  942. finish_wait(&cur_trans->writer_wait, &wait);
  943. } while (cur_trans->num_writers > 1 ||
  944. (should_grow && cur_trans->num_joined != joined));
  945. ret = create_pending_snapshots(trans, root->fs_info);
  946. BUG_ON(ret);
  947. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  948. BUG_ON(ret);
  949. WARN_ON(cur_trans != trans->transaction);
  950. /* btrfs_commit_tree_roots is responsible for getting the
  951. * various roots consistent with each other. Every pointer
  952. * in the tree of tree roots has to point to the most up to date
  953. * root for every subvolume and other tree. So, we have to keep
  954. * the tree logging code from jumping in and changing any
  955. * of the trees.
  956. *
  957. * At this point in the commit, there can't be any tree-log
  958. * writers, but a little lower down we drop the trans mutex
  959. * and let new people in. By holding the tree_log_mutex
  960. * from now until after the super is written, we avoid races
  961. * with the tree-log code.
  962. */
  963. mutex_lock(&root->fs_info->tree_log_mutex);
  964. ret = commit_fs_roots(trans, root);
  965. BUG_ON(ret);
  966. /* commit_fs_roots gets rid of all the tree log roots, it is now
  967. * safe to free the root of tree log roots
  968. */
  969. btrfs_free_log_root_tree(trans, root->fs_info);
  970. ret = commit_cowonly_roots(trans, root);
  971. BUG_ON(ret);
  972. btrfs_prepare_extent_commit(trans, root);
  973. cur_trans = root->fs_info->running_transaction;
  974. spin_lock(&root->fs_info->new_trans_lock);
  975. root->fs_info->running_transaction = NULL;
  976. spin_unlock(&root->fs_info->new_trans_lock);
  977. btrfs_set_root_node(&root->fs_info->tree_root->root_item,
  978. root->fs_info->tree_root->node);
  979. switch_commit_root(root->fs_info->tree_root);
  980. btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
  981. root->fs_info->chunk_root->node);
  982. switch_commit_root(root->fs_info->chunk_root);
  983. update_super_roots(root);
  984. if (!root->fs_info->log_root_recovering) {
  985. btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
  986. btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
  987. }
  988. memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
  989. sizeof(root->fs_info->super_copy));
  990. trans->transaction->blocked = 0;
  991. wake_up(&root->fs_info->transaction_wait);
  992. mutex_unlock(&root->fs_info->trans_mutex);
  993. ret = btrfs_write_and_wait_transaction(trans, root);
  994. BUG_ON(ret);
  995. write_ctree_super(trans, root, 0);
  996. /*
  997. * the super is written, we can safely allow the tree-loggers
  998. * to go about their business
  999. */
  1000. mutex_unlock(&root->fs_info->tree_log_mutex);
  1001. btrfs_finish_extent_commit(trans, root);
  1002. mutex_lock(&root->fs_info->trans_mutex);
  1003. cur_trans->commit_done = 1;
  1004. root->fs_info->last_trans_committed = cur_trans->transid;
  1005. wake_up(&cur_trans->commit_wait);
  1006. put_transaction(cur_trans);
  1007. put_transaction(cur_trans);
  1008. mutex_unlock(&root->fs_info->trans_mutex);
  1009. if (current->journal_info == trans)
  1010. current->journal_info = NULL;
  1011. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1012. if (current != root->fs_info->transaction_kthread)
  1013. btrfs_run_delayed_iputs(root);
  1014. return ret;
  1015. }
  1016. /*
  1017. * interface function to delete all the snapshots we have scheduled for deletion
  1018. */
  1019. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  1020. {
  1021. LIST_HEAD(list);
  1022. struct btrfs_fs_info *fs_info = root->fs_info;
  1023. mutex_lock(&fs_info->trans_mutex);
  1024. list_splice_init(&fs_info->dead_roots, &list);
  1025. mutex_unlock(&fs_info->trans_mutex);
  1026. while (!list_empty(&list)) {
  1027. root = list_entry(list.next, struct btrfs_root, root_list);
  1028. list_del(&root->root_list);
  1029. if (btrfs_header_backref_rev(root->node) <
  1030. BTRFS_MIXED_BACKREF_REV)
  1031. btrfs_drop_snapshot(root, NULL, 0);
  1032. else
  1033. btrfs_drop_snapshot(root, NULL, 1);
  1034. }
  1035. return 0;
  1036. }