transaction.c 37 KB

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