transaction.c 29 KB

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