transaction.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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 "ctree.h"
  23. #include "disk-io.h"
  24. #include "transaction.h"
  25. #include "locking.h"
  26. #include "ref-cache.h"
  27. #include "tree-log.h"
  28. static int total_trans = 0;
  29. extern struct kmem_cache *btrfs_trans_handle_cachep;
  30. extern struct kmem_cache *btrfs_transaction_cachep;
  31. #define BTRFS_ROOT_TRANS_TAG 0
  32. static noinline void put_transaction(struct btrfs_transaction *transaction)
  33. {
  34. WARN_ON(transaction->use_count == 0);
  35. transaction->use_count--;
  36. if (transaction->use_count == 0) {
  37. WARN_ON(total_trans == 0);
  38. total_trans--;
  39. list_del_init(&transaction->list);
  40. memset(transaction, 0, sizeof(*transaction));
  41. kmem_cache_free(btrfs_transaction_cachep, transaction);
  42. }
  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. total_trans++;
  55. BUG_ON(!cur_trans);
  56. root->fs_info->generation++;
  57. root->fs_info->last_alloc = 0;
  58. root->fs_info->last_data_alloc = 0;
  59. cur_trans->num_writers = 1;
  60. cur_trans->num_joined = 0;
  61. cur_trans->transid = root->fs_info->generation;
  62. init_waitqueue_head(&cur_trans->writer_wait);
  63. init_waitqueue_head(&cur_trans->commit_wait);
  64. cur_trans->in_commit = 0;
  65. cur_trans->blocked = 0;
  66. cur_trans->use_count = 1;
  67. cur_trans->commit_done = 0;
  68. cur_trans->start_time = get_seconds();
  69. INIT_LIST_HEAD(&cur_trans->pending_snapshots);
  70. list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
  71. extent_io_tree_init(&cur_trans->dirty_pages,
  72. root->fs_info->btree_inode->i_mapping,
  73. GFP_NOFS);
  74. spin_lock(&root->fs_info->new_trans_lock);
  75. root->fs_info->running_transaction = cur_trans;
  76. spin_unlock(&root->fs_info->new_trans_lock);
  77. } else {
  78. cur_trans->num_writers++;
  79. cur_trans->num_joined++;
  80. }
  81. return 0;
  82. }
  83. /*
  84. * this does all the record keeping required to make sure that a
  85. * reference counted root is properly recorded in a given transaction.
  86. * This is required to make sure the old root from before we joined the transaction
  87. * is deleted when the transaction commits
  88. */
  89. noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
  90. {
  91. struct btrfs_dirty_root *dirty;
  92. u64 running_trans_id = root->fs_info->running_transaction->transid;
  93. if (root->ref_cows && root->last_trans < running_trans_id) {
  94. WARN_ON(root == root->fs_info->extent_root);
  95. if (root->root_item.refs != 0) {
  96. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  97. (unsigned long)root->root_key.objectid,
  98. BTRFS_ROOT_TRANS_TAG);
  99. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  100. BUG_ON(!dirty);
  101. dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
  102. BUG_ON(!dirty->root);
  103. dirty->latest_root = root;
  104. INIT_LIST_HEAD(&dirty->list);
  105. root->commit_root = btrfs_root_node(root);
  106. memcpy(dirty->root, root, sizeof(*root));
  107. spin_lock_init(&dirty->root->node_lock);
  108. spin_lock_init(&dirty->root->list_lock);
  109. mutex_init(&dirty->root->objectid_mutex);
  110. mutex_init(&dirty->root->log_mutex);
  111. INIT_LIST_HEAD(&dirty->root->dead_list);
  112. dirty->root->node = root->commit_root;
  113. dirty->root->commit_root = NULL;
  114. spin_lock(&root->list_lock);
  115. list_add(&dirty->root->dead_list, &root->dead_list);
  116. spin_unlock(&root->list_lock);
  117. root->dirty_root = dirty;
  118. } else {
  119. WARN_ON(1);
  120. }
  121. root->last_trans = running_trans_id;
  122. }
  123. return 0;
  124. }
  125. /* wait for commit against the current transaction to become unblocked
  126. * when this is done, it is safe to start a new transaction, but the current
  127. * transaction might not be fully on disk.
  128. */
  129. static void wait_current_trans(struct btrfs_root *root)
  130. {
  131. struct btrfs_transaction *cur_trans;
  132. cur_trans = root->fs_info->running_transaction;
  133. if (cur_trans && cur_trans->blocked) {
  134. DEFINE_WAIT(wait);
  135. cur_trans->use_count++;
  136. while(1) {
  137. prepare_to_wait(&root->fs_info->transaction_wait, &wait,
  138. TASK_UNINTERRUPTIBLE);
  139. if (cur_trans->blocked) {
  140. mutex_unlock(&root->fs_info->trans_mutex);
  141. schedule();
  142. mutex_lock(&root->fs_info->trans_mutex);
  143. finish_wait(&root->fs_info->transaction_wait,
  144. &wait);
  145. } else {
  146. finish_wait(&root->fs_info->transaction_wait,
  147. &wait);
  148. break;
  149. }
  150. }
  151. put_transaction(cur_trans);
  152. }
  153. }
  154. static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
  155. int num_blocks, int wait)
  156. {
  157. struct btrfs_trans_handle *h =
  158. kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  159. int ret;
  160. mutex_lock(&root->fs_info->trans_mutex);
  161. if (!root->fs_info->log_root_recovering &&
  162. ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
  163. wait_current_trans(root);
  164. ret = join_transaction(root);
  165. BUG_ON(ret);
  166. btrfs_record_root_in_trans(root);
  167. h->transid = root->fs_info->running_transaction->transid;
  168. h->transaction = root->fs_info->running_transaction;
  169. h->blocks_reserved = num_blocks;
  170. h->blocks_used = 0;
  171. h->block_group = NULL;
  172. h->alloc_exclude_nr = 0;
  173. h->alloc_exclude_start = 0;
  174. root->fs_info->running_transaction->use_count++;
  175. mutex_unlock(&root->fs_info->trans_mutex);
  176. return h;
  177. }
  178. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  179. int num_blocks)
  180. {
  181. return start_transaction(root, num_blocks, 1);
  182. }
  183. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
  184. int num_blocks)
  185. {
  186. return start_transaction(root, num_blocks, 0);
  187. }
  188. struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
  189. int num_blocks)
  190. {
  191. return start_transaction(r, num_blocks, 2);
  192. }
  193. /* wait for a transaction commit to be fully complete */
  194. static noinline int wait_for_commit(struct btrfs_root *root,
  195. struct btrfs_transaction *commit)
  196. {
  197. DEFINE_WAIT(wait);
  198. mutex_lock(&root->fs_info->trans_mutex);
  199. while(!commit->commit_done) {
  200. prepare_to_wait(&commit->commit_wait, &wait,
  201. TASK_UNINTERRUPTIBLE);
  202. if (commit->commit_done)
  203. break;
  204. mutex_unlock(&root->fs_info->trans_mutex);
  205. schedule();
  206. mutex_lock(&root->fs_info->trans_mutex);
  207. }
  208. mutex_unlock(&root->fs_info->trans_mutex);
  209. finish_wait(&commit->commit_wait, &wait);
  210. return 0;
  211. }
  212. /*
  213. * rate limit against the drop_snapshot code. This helps to slow down new operations
  214. * if the drop_snapshot code isn't able to keep up.
  215. */
  216. static void throttle_on_drops(struct btrfs_root *root)
  217. {
  218. struct btrfs_fs_info *info = root->fs_info;
  219. int harder_count = 0;
  220. harder:
  221. if (atomic_read(&info->throttles)) {
  222. DEFINE_WAIT(wait);
  223. int thr;
  224. thr = atomic_read(&info->throttle_gen);
  225. do {
  226. prepare_to_wait(&info->transaction_throttle,
  227. &wait, TASK_UNINTERRUPTIBLE);
  228. if (!atomic_read(&info->throttles)) {
  229. finish_wait(&info->transaction_throttle, &wait);
  230. break;
  231. }
  232. schedule();
  233. finish_wait(&info->transaction_throttle, &wait);
  234. } while (thr == atomic_read(&info->throttle_gen));
  235. harder_count++;
  236. if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
  237. harder_count < 2)
  238. goto harder;
  239. if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
  240. harder_count < 10)
  241. goto harder;
  242. if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
  243. harder_count < 20)
  244. goto harder;
  245. }
  246. }
  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. throttle_on_drops(root);
  254. }
  255. static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
  256. struct btrfs_root *root, int throttle)
  257. {
  258. struct btrfs_transaction *cur_trans;
  259. struct btrfs_fs_info *info = root->fs_info;
  260. mutex_lock(&info->trans_mutex);
  261. cur_trans = info->running_transaction;
  262. WARN_ON(cur_trans != trans->transaction);
  263. WARN_ON(cur_trans->num_writers < 1);
  264. cur_trans->num_writers--;
  265. if (waitqueue_active(&cur_trans->writer_wait))
  266. wake_up(&cur_trans->writer_wait);
  267. put_transaction(cur_trans);
  268. mutex_unlock(&info->trans_mutex);
  269. memset(trans, 0, sizeof(*trans));
  270. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  271. if (throttle)
  272. throttle_on_drops(root);
  273. return 0;
  274. }
  275. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  276. struct btrfs_root *root)
  277. {
  278. return __btrfs_end_transaction(trans, root, 0);
  279. }
  280. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  281. struct btrfs_root *root)
  282. {
  283. return __btrfs_end_transaction(trans, root, 1);
  284. }
  285. /*
  286. * when btree blocks are allocated, they have some corresponding bits set for
  287. * them in one of two extent_io trees. This is used to make sure all of
  288. * those extents are on disk for transaction or log commit
  289. */
  290. int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
  291. struct extent_io_tree *dirty_pages)
  292. {
  293. int ret;
  294. int err = 0;
  295. int werr = 0;
  296. struct page *page;
  297. struct inode *btree_inode = root->fs_info->btree_inode;
  298. u64 start = 0;
  299. u64 end;
  300. unsigned long index;
  301. while(1) {
  302. ret = find_first_extent_bit(dirty_pages, start, &start, &end,
  303. EXTENT_DIRTY);
  304. if (ret)
  305. break;
  306. while(start <= end) {
  307. cond_resched();
  308. index = start >> PAGE_CACHE_SHIFT;
  309. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  310. page = find_get_page(btree_inode->i_mapping, index);
  311. if (!page)
  312. continue;
  313. btree_lock_page_hook(page);
  314. if (!page->mapping) {
  315. unlock_page(page);
  316. page_cache_release(page);
  317. continue;
  318. }
  319. if (PageWriteback(page)) {
  320. if (PageDirty(page))
  321. wait_on_page_writeback(page);
  322. else {
  323. unlock_page(page);
  324. page_cache_release(page);
  325. continue;
  326. }
  327. }
  328. err = write_one_page(page, 0);
  329. if (err)
  330. werr = err;
  331. page_cache_release(page);
  332. }
  333. }
  334. while(1) {
  335. ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
  336. EXTENT_DIRTY);
  337. if (ret)
  338. break;
  339. clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
  340. while(start <= end) {
  341. index = start >> PAGE_CACHE_SHIFT;
  342. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  343. page = find_get_page(btree_inode->i_mapping, index);
  344. if (!page)
  345. continue;
  346. if (PageDirty(page)) {
  347. btree_lock_page_hook(page);
  348. wait_on_page_writeback(page);
  349. err = write_one_page(page, 0);
  350. if (err)
  351. werr = err;
  352. }
  353. wait_on_page_writeback(page);
  354. page_cache_release(page);
  355. cond_resched();
  356. }
  357. }
  358. if (err)
  359. werr = err;
  360. return werr;
  361. }
  362. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  363. struct btrfs_root *root)
  364. {
  365. if (!trans || !trans->transaction) {
  366. struct inode *btree_inode;
  367. btree_inode = root->fs_info->btree_inode;
  368. return filemap_write_and_wait(btree_inode->i_mapping);
  369. }
  370. return btrfs_write_and_wait_marked_extents(root,
  371. &trans->transaction->dirty_pages);
  372. }
  373. /*
  374. * this is used to update the root pointer in the tree of tree roots.
  375. *
  376. * But, in the case of the extent allocation tree, updating the root
  377. * pointer may allocate blocks which may change the root of the extent
  378. * allocation tree.
  379. *
  380. * So, this loops and repeats and makes sure the cowonly root didn't
  381. * change while the root pointer was being updated in the metadata.
  382. */
  383. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  384. struct btrfs_root *root)
  385. {
  386. int ret;
  387. u64 old_root_bytenr;
  388. struct btrfs_root *tree_root = root->fs_info->tree_root;
  389. btrfs_write_dirty_block_groups(trans, root);
  390. while(1) {
  391. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  392. if (old_root_bytenr == root->node->start)
  393. break;
  394. btrfs_set_root_bytenr(&root->root_item,
  395. root->node->start);
  396. btrfs_set_root_level(&root->root_item,
  397. btrfs_header_level(root->node));
  398. ret = btrfs_update_root(trans, tree_root,
  399. &root->root_key,
  400. &root->root_item);
  401. BUG_ON(ret);
  402. btrfs_write_dirty_block_groups(trans, root);
  403. }
  404. return 0;
  405. }
  406. /*
  407. * update all the cowonly tree roots on disk
  408. */
  409. int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
  410. struct btrfs_root *root)
  411. {
  412. struct btrfs_fs_info *fs_info = root->fs_info;
  413. struct list_head *next;
  414. while(!list_empty(&fs_info->dirty_cowonly_roots)) {
  415. next = fs_info->dirty_cowonly_roots.next;
  416. list_del_init(next);
  417. root = list_entry(next, struct btrfs_root, dirty_list);
  418. update_cowonly_root(trans, root);
  419. }
  420. return 0;
  421. }
  422. /*
  423. * dead roots are old snapshots that need to be deleted. This allocates
  424. * a dirty root struct and adds it into the list of dead roots that need to
  425. * be deleted
  426. */
  427. int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
  428. {
  429. struct btrfs_dirty_root *dirty;
  430. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  431. if (!dirty)
  432. return -ENOMEM;
  433. dirty->root = root;
  434. dirty->latest_root = latest;
  435. mutex_lock(&root->fs_info->trans_mutex);
  436. list_add(&dirty->list, &latest->fs_info->dead_roots);
  437. mutex_unlock(&root->fs_info->trans_mutex);
  438. return 0;
  439. }
  440. /*
  441. * at transaction commit time we need to schedule the old roots for
  442. * deletion via btrfs_drop_snapshot. This runs through all the
  443. * reference counted roots that were modified in the current
  444. * transaction and puts them into the drop list
  445. */
  446. static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
  447. struct radix_tree_root *radix,
  448. struct list_head *list)
  449. {
  450. struct btrfs_dirty_root *dirty;
  451. struct btrfs_root *gang[8];
  452. struct btrfs_root *root;
  453. int i;
  454. int ret;
  455. int err = 0;
  456. u32 refs;
  457. while(1) {
  458. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  459. ARRAY_SIZE(gang),
  460. BTRFS_ROOT_TRANS_TAG);
  461. if (ret == 0)
  462. break;
  463. for (i = 0; i < ret; i++) {
  464. root = gang[i];
  465. radix_tree_tag_clear(radix,
  466. (unsigned long)root->root_key.objectid,
  467. BTRFS_ROOT_TRANS_TAG);
  468. BUG_ON(!root->ref_tree);
  469. dirty = root->dirty_root;
  470. btrfs_free_log(trans, root);
  471. btrfs_free_reloc_root(trans, root);
  472. if (root->commit_root == root->node) {
  473. WARN_ON(root->node->start !=
  474. btrfs_root_bytenr(&root->root_item));
  475. free_extent_buffer(root->commit_root);
  476. root->commit_root = NULL;
  477. root->dirty_root = NULL;
  478. spin_lock(&root->list_lock);
  479. list_del_init(&dirty->root->dead_list);
  480. spin_unlock(&root->list_lock);
  481. kfree(dirty->root);
  482. kfree(dirty);
  483. /* make sure to update the root on disk
  484. * so we get any updates to the block used
  485. * counts
  486. */
  487. err = btrfs_update_root(trans,
  488. root->fs_info->tree_root,
  489. &root->root_key,
  490. &root->root_item);
  491. continue;
  492. }
  493. memset(&root->root_item.drop_progress, 0,
  494. sizeof(struct btrfs_disk_key));
  495. root->root_item.drop_level = 0;
  496. root->commit_root = NULL;
  497. root->dirty_root = NULL;
  498. root->root_key.offset = root->fs_info->generation;
  499. btrfs_set_root_bytenr(&root->root_item,
  500. root->node->start);
  501. btrfs_set_root_level(&root->root_item,
  502. btrfs_header_level(root->node));
  503. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  504. &root->root_key,
  505. &root->root_item);
  506. if (err)
  507. break;
  508. refs = btrfs_root_refs(&dirty->root->root_item);
  509. btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
  510. err = btrfs_update_root(trans, root->fs_info->tree_root,
  511. &dirty->root->root_key,
  512. &dirty->root->root_item);
  513. BUG_ON(err);
  514. if (refs == 1) {
  515. list_add(&dirty->list, list);
  516. } else {
  517. WARN_ON(1);
  518. free_extent_buffer(dirty->root->node);
  519. kfree(dirty->root);
  520. kfree(dirty);
  521. }
  522. }
  523. }
  524. return err;
  525. }
  526. /*
  527. * defrag a given btree. If cacheonly == 1, this won't read from the disk,
  528. * otherwise every leaf in the btree is read and defragged.
  529. */
  530. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  531. {
  532. struct btrfs_fs_info *info = root->fs_info;
  533. int ret;
  534. struct btrfs_trans_handle *trans;
  535. unsigned long nr;
  536. smp_mb();
  537. if (root->defrag_running)
  538. return 0;
  539. trans = btrfs_start_transaction(root, 1);
  540. while (1) {
  541. root->defrag_running = 1;
  542. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  543. nr = trans->blocks_used;
  544. btrfs_end_transaction(trans, root);
  545. btrfs_btree_balance_dirty(info->tree_root, nr);
  546. cond_resched();
  547. trans = btrfs_start_transaction(root, 1);
  548. if (root->fs_info->closing || ret != -EAGAIN)
  549. break;
  550. }
  551. root->defrag_running = 0;
  552. smp_mb();
  553. btrfs_end_transaction(trans, root);
  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. static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
  561. struct list_head *list)
  562. {
  563. struct btrfs_dirty_root *dirty;
  564. struct btrfs_trans_handle *trans;
  565. unsigned long nr;
  566. u64 num_bytes;
  567. u64 bytes_used;
  568. u64 max_useless;
  569. int ret = 0;
  570. int err;
  571. while(!list_empty(list)) {
  572. struct btrfs_root *root;
  573. dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
  574. list_del_init(&dirty->list);
  575. num_bytes = btrfs_root_used(&dirty->root->root_item);
  576. root = dirty->latest_root;
  577. atomic_inc(&root->fs_info->throttles);
  578. while(1) {
  579. trans = btrfs_start_transaction(tree_root, 1);
  580. mutex_lock(&root->fs_info->drop_mutex);
  581. ret = btrfs_drop_snapshot(trans, dirty->root);
  582. if (ret != -EAGAIN) {
  583. break;
  584. }
  585. mutex_unlock(&root->fs_info->drop_mutex);
  586. err = btrfs_update_root(trans,
  587. tree_root,
  588. &dirty->root->root_key,
  589. &dirty->root->root_item);
  590. if (err)
  591. ret = err;
  592. nr = trans->blocks_used;
  593. ret = btrfs_end_transaction(trans, tree_root);
  594. BUG_ON(ret);
  595. btrfs_btree_balance_dirty(tree_root, nr);
  596. cond_resched();
  597. }
  598. BUG_ON(ret);
  599. atomic_dec(&root->fs_info->throttles);
  600. wake_up(&root->fs_info->transaction_throttle);
  601. num_bytes -= btrfs_root_used(&dirty->root->root_item);
  602. bytes_used = btrfs_root_used(&root->root_item);
  603. if (num_bytes) {
  604. btrfs_record_root_in_trans(root);
  605. btrfs_set_root_used(&root->root_item,
  606. bytes_used - num_bytes);
  607. }
  608. ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
  609. if (ret) {
  610. BUG();
  611. break;
  612. }
  613. mutex_unlock(&root->fs_info->drop_mutex);
  614. spin_lock(&root->list_lock);
  615. list_del_init(&dirty->root->dead_list);
  616. if (!list_empty(&root->dead_list)) {
  617. struct btrfs_root *oldest;
  618. oldest = list_entry(root->dead_list.prev,
  619. struct btrfs_root, dead_list);
  620. max_useless = oldest->root_key.offset - 1;
  621. } else {
  622. max_useless = root->root_key.offset - 1;
  623. }
  624. spin_unlock(&root->list_lock);
  625. nr = trans->blocks_used;
  626. ret = btrfs_end_transaction(trans, tree_root);
  627. BUG_ON(ret);
  628. ret = btrfs_remove_leaf_refs(root, max_useless, 0);
  629. BUG_ON(ret);
  630. free_extent_buffer(dirty->root->node);
  631. kfree(dirty->root);
  632. kfree(dirty);
  633. btrfs_btree_balance_dirty(tree_root, nr);
  634. cond_resched();
  635. }
  636. return ret;
  637. }
  638. /*
  639. * new snapshots need to be created at a very specific time in the
  640. * transaction commit. This does the actual creation
  641. */
  642. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  643. struct btrfs_fs_info *fs_info,
  644. struct btrfs_pending_snapshot *pending)
  645. {
  646. struct btrfs_key key;
  647. struct btrfs_root_item *new_root_item;
  648. struct btrfs_root *tree_root = fs_info->tree_root;
  649. struct btrfs_root *root = pending->root;
  650. struct extent_buffer *tmp;
  651. struct extent_buffer *old;
  652. int ret;
  653. int namelen;
  654. u64 objectid;
  655. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  656. if (!new_root_item) {
  657. ret = -ENOMEM;
  658. goto fail;
  659. }
  660. ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
  661. if (ret)
  662. goto fail;
  663. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  664. key.objectid = objectid;
  665. key.offset = trans->transid;
  666. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  667. old = btrfs_lock_root_node(root);
  668. btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
  669. btrfs_copy_root(trans, root, old, &tmp, objectid);
  670. btrfs_tree_unlock(old);
  671. free_extent_buffer(old);
  672. btrfs_set_root_bytenr(new_root_item, tmp->start);
  673. btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
  674. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  675. new_root_item);
  676. btrfs_tree_unlock(tmp);
  677. free_extent_buffer(tmp);
  678. if (ret)
  679. goto fail;
  680. /*
  681. * insert the directory item
  682. */
  683. key.offset = (u64)-1;
  684. namelen = strlen(pending->name);
  685. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  686. pending->name, namelen,
  687. root->fs_info->sb->s_root->d_inode->i_ino,
  688. &key, BTRFS_FT_DIR, 0);
  689. if (ret)
  690. goto fail;
  691. ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
  692. pending->name, strlen(pending->name), objectid,
  693. root->fs_info->sb->s_root->d_inode->i_ino, 0);
  694. /* Invalidate existing dcache entry for new snapshot. */
  695. btrfs_invalidate_dcache_root(root, pending->name, namelen);
  696. fail:
  697. kfree(new_root_item);
  698. return ret;
  699. }
  700. /*
  701. * create all the snapshots we've scheduled for creation
  702. */
  703. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  704. struct btrfs_fs_info *fs_info)
  705. {
  706. struct btrfs_pending_snapshot *pending;
  707. struct list_head *head = &trans->transaction->pending_snapshots;
  708. int ret;
  709. while(!list_empty(head)) {
  710. pending = list_entry(head->next,
  711. struct btrfs_pending_snapshot, list);
  712. ret = create_pending_snapshot(trans, fs_info, pending);
  713. BUG_ON(ret);
  714. list_del(&pending->list);
  715. kfree(pending->name);
  716. kfree(pending);
  717. }
  718. return 0;
  719. }
  720. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  721. struct btrfs_root *root)
  722. {
  723. unsigned long joined = 0;
  724. unsigned long timeout = 1;
  725. struct btrfs_transaction *cur_trans;
  726. struct btrfs_transaction *prev_trans = NULL;
  727. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  728. struct list_head dirty_fs_roots;
  729. struct extent_io_tree *pinned_copy;
  730. DEFINE_WAIT(wait);
  731. int ret;
  732. INIT_LIST_HEAD(&dirty_fs_roots);
  733. mutex_lock(&root->fs_info->trans_mutex);
  734. if (trans->transaction->in_commit) {
  735. cur_trans = trans->transaction;
  736. trans->transaction->use_count++;
  737. mutex_unlock(&root->fs_info->trans_mutex);
  738. btrfs_end_transaction(trans, root);
  739. ret = wait_for_commit(root, cur_trans);
  740. BUG_ON(ret);
  741. mutex_lock(&root->fs_info->trans_mutex);
  742. put_transaction(cur_trans);
  743. mutex_unlock(&root->fs_info->trans_mutex);
  744. return 0;
  745. }
  746. pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
  747. if (!pinned_copy)
  748. return -ENOMEM;
  749. extent_io_tree_init(pinned_copy,
  750. root->fs_info->btree_inode->i_mapping, GFP_NOFS);
  751. trans->transaction->in_commit = 1;
  752. trans->transaction->blocked = 1;
  753. cur_trans = trans->transaction;
  754. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  755. prev_trans = list_entry(cur_trans->list.prev,
  756. struct btrfs_transaction, list);
  757. if (!prev_trans->commit_done) {
  758. prev_trans->use_count++;
  759. mutex_unlock(&root->fs_info->trans_mutex);
  760. wait_for_commit(root, prev_trans);
  761. mutex_lock(&root->fs_info->trans_mutex);
  762. put_transaction(prev_trans);
  763. }
  764. }
  765. do {
  766. int snap_pending = 0;
  767. joined = cur_trans->num_joined;
  768. if (!list_empty(&trans->transaction->pending_snapshots))
  769. snap_pending = 1;
  770. WARN_ON(cur_trans != trans->transaction);
  771. prepare_to_wait(&cur_trans->writer_wait, &wait,
  772. TASK_UNINTERRUPTIBLE);
  773. if (cur_trans->num_writers > 1)
  774. timeout = MAX_SCHEDULE_TIMEOUT;
  775. else
  776. timeout = 1;
  777. mutex_unlock(&root->fs_info->trans_mutex);
  778. if (snap_pending) {
  779. ret = btrfs_wait_ordered_extents(root, 1);
  780. BUG_ON(ret);
  781. }
  782. schedule_timeout(timeout);
  783. mutex_lock(&root->fs_info->trans_mutex);
  784. finish_wait(&cur_trans->writer_wait, &wait);
  785. } while (cur_trans->num_writers > 1 ||
  786. (cur_trans->num_joined != joined));
  787. ret = create_pending_snapshots(trans, root->fs_info);
  788. BUG_ON(ret);
  789. WARN_ON(cur_trans != trans->transaction);
  790. /* btrfs_commit_tree_roots is responsible for getting the
  791. * various roots consistent with each other. Every pointer
  792. * in the tree of tree roots has to point to the most up to date
  793. * root for every subvolume and other tree. So, we have to keep
  794. * the tree logging code from jumping in and changing any
  795. * of the trees.
  796. *
  797. * At this point in the commit, there can't be any tree-log
  798. * writers, but a little lower down we drop the trans mutex
  799. * and let new people in. By holding the tree_log_mutex
  800. * from now until after the super is written, we avoid races
  801. * with the tree-log code.
  802. */
  803. mutex_lock(&root->fs_info->tree_log_mutex);
  804. /*
  805. * keep tree reloc code from adding new reloc trees
  806. */
  807. mutex_lock(&root->fs_info->tree_reloc_mutex);
  808. ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
  809. &dirty_fs_roots);
  810. BUG_ON(ret);
  811. /* add_dirty_roots gets rid of all the tree log roots, it is now
  812. * safe to free the root of tree log roots
  813. */
  814. btrfs_free_log_root_tree(trans, root->fs_info);
  815. ret = btrfs_commit_tree_roots(trans, root);
  816. BUG_ON(ret);
  817. cur_trans = root->fs_info->running_transaction;
  818. spin_lock(&root->fs_info->new_trans_lock);
  819. root->fs_info->running_transaction = NULL;
  820. spin_unlock(&root->fs_info->new_trans_lock);
  821. btrfs_set_super_generation(&root->fs_info->super_copy,
  822. cur_trans->transid);
  823. btrfs_set_super_root(&root->fs_info->super_copy,
  824. root->fs_info->tree_root->node->start);
  825. btrfs_set_super_root_level(&root->fs_info->super_copy,
  826. btrfs_header_level(root->fs_info->tree_root->node));
  827. btrfs_set_super_chunk_root(&root->fs_info->super_copy,
  828. chunk_root->node->start);
  829. btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
  830. btrfs_header_level(chunk_root->node));
  831. if (!root->fs_info->log_root_recovering) {
  832. btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
  833. btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
  834. }
  835. memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
  836. sizeof(root->fs_info->super_copy));
  837. btrfs_copy_pinned(root, pinned_copy);
  838. trans->transaction->blocked = 0;
  839. wake_up(&root->fs_info->transaction_throttle);
  840. wake_up(&root->fs_info->transaction_wait);
  841. mutex_unlock(&root->fs_info->trans_mutex);
  842. ret = btrfs_write_and_wait_transaction(trans, root);
  843. BUG_ON(ret);
  844. write_ctree_super(trans, root);
  845. /*
  846. * the super is written, we can safely allow the tree-loggers
  847. * to go about their business
  848. */
  849. mutex_unlock(&root->fs_info->tree_log_mutex);
  850. btrfs_finish_extent_commit(trans, root, pinned_copy);
  851. kfree(pinned_copy);
  852. btrfs_drop_dead_reloc_roots(root);
  853. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  854. mutex_lock(&root->fs_info->trans_mutex);
  855. cur_trans->commit_done = 1;
  856. root->fs_info->last_trans_committed = cur_trans->transid;
  857. wake_up(&cur_trans->commit_wait);
  858. put_transaction(cur_trans);
  859. put_transaction(cur_trans);
  860. list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
  861. if (root->fs_info->closing)
  862. list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
  863. mutex_unlock(&root->fs_info->trans_mutex);
  864. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  865. if (root->fs_info->closing) {
  866. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  867. }
  868. return ret;
  869. }
  870. /*
  871. * interface function to delete all the snapshots we have scheduled for deletion
  872. */
  873. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  874. {
  875. struct list_head dirty_roots;
  876. INIT_LIST_HEAD(&dirty_roots);
  877. again:
  878. mutex_lock(&root->fs_info->trans_mutex);
  879. list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
  880. mutex_unlock(&root->fs_info->trans_mutex);
  881. if (!list_empty(&dirty_roots)) {
  882. drop_dirty_roots(root, &dirty_roots);
  883. goto again;
  884. }
  885. return 0;
  886. }