transaction.c 32 KB

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