transaction.c 37 KB

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