transaction.c 36 KB

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