transaction.c 37 KB

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