transaction.c 37 KB

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