transaction.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  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 <linux/uuid.h>
  25. #include "ctree.h"
  26. #include "disk-io.h"
  27. #include "transaction.h"
  28. #include "locking.h"
  29. #include "tree-log.h"
  30. #include "inode-map.h"
  31. #include "volumes.h"
  32. #define BTRFS_ROOT_TRANS_TAG 0
  33. void put_transaction(struct btrfs_transaction *transaction)
  34. {
  35. WARN_ON(atomic_read(&transaction->use_count) == 0);
  36. if (atomic_dec_and_test(&transaction->use_count)) {
  37. BUG_ON(!list_empty(&transaction->list));
  38. WARN_ON(transaction->delayed_refs.root.rb_node);
  39. memset(transaction, 0, sizeof(*transaction));
  40. kmem_cache_free(btrfs_transaction_cachep, transaction);
  41. }
  42. }
  43. static noinline void switch_commit_root(struct btrfs_root *root)
  44. {
  45. free_extent_buffer(root->commit_root);
  46. root->commit_root = btrfs_root_node(root);
  47. }
  48. /*
  49. * either allocate a new transaction or hop into the existing one
  50. */
  51. static noinline int join_transaction(struct btrfs_root *root, int type)
  52. {
  53. struct btrfs_transaction *cur_trans;
  54. struct btrfs_fs_info *fs_info = root->fs_info;
  55. spin_lock(&fs_info->trans_lock);
  56. loop:
  57. /* The file system has been taken offline. No new transactions. */
  58. if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
  59. spin_unlock(&fs_info->trans_lock);
  60. return -EROFS;
  61. }
  62. if (fs_info->trans_no_join) {
  63. /*
  64. * If we are JOIN_NOLOCK we're already committing a current
  65. * transaction, we just need a handle to deal with something
  66. * when committing the transaction, such as inode cache and
  67. * space cache. It is a special case.
  68. */
  69. if (type != TRANS_JOIN_NOLOCK) {
  70. spin_unlock(&fs_info->trans_lock);
  71. return -EBUSY;
  72. }
  73. }
  74. cur_trans = fs_info->running_transaction;
  75. if (cur_trans) {
  76. if (cur_trans->aborted) {
  77. spin_unlock(&fs_info->trans_lock);
  78. return cur_trans->aborted;
  79. }
  80. atomic_inc(&cur_trans->use_count);
  81. atomic_inc(&cur_trans->num_writers);
  82. cur_trans->num_joined++;
  83. spin_unlock(&fs_info->trans_lock);
  84. return 0;
  85. }
  86. spin_unlock(&fs_info->trans_lock);
  87. /*
  88. * If we are ATTACH, we just want to catch the current transaction,
  89. * and commit it. If there is no transaction, just return ENOENT.
  90. */
  91. if (type == TRANS_ATTACH)
  92. return -ENOENT;
  93. cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
  94. if (!cur_trans)
  95. return -ENOMEM;
  96. spin_lock(&fs_info->trans_lock);
  97. if (fs_info->running_transaction) {
  98. /*
  99. * someone started a transaction after we unlocked. Make sure
  100. * to redo the trans_no_join checks above
  101. */
  102. kmem_cache_free(btrfs_transaction_cachep, cur_trans);
  103. cur_trans = fs_info->running_transaction;
  104. goto loop;
  105. } else if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
  106. spin_unlock(&fs_info->trans_lock);
  107. kmem_cache_free(btrfs_transaction_cachep, cur_trans);
  108. return -EROFS;
  109. }
  110. atomic_set(&cur_trans->num_writers, 1);
  111. cur_trans->num_joined = 0;
  112. init_waitqueue_head(&cur_trans->writer_wait);
  113. init_waitqueue_head(&cur_trans->commit_wait);
  114. cur_trans->in_commit = 0;
  115. cur_trans->blocked = 0;
  116. /*
  117. * One for this trans handle, one so it will live on until we
  118. * commit the transaction.
  119. */
  120. atomic_set(&cur_trans->use_count, 2);
  121. cur_trans->commit_done = 0;
  122. cur_trans->start_time = get_seconds();
  123. cur_trans->delayed_refs.root = RB_ROOT;
  124. cur_trans->delayed_refs.num_entries = 0;
  125. cur_trans->delayed_refs.num_heads_ready = 0;
  126. cur_trans->delayed_refs.num_heads = 0;
  127. cur_trans->delayed_refs.flushing = 0;
  128. cur_trans->delayed_refs.run_delayed_start = 0;
  129. /*
  130. * although the tree mod log is per file system and not per transaction,
  131. * the log must never go across transaction boundaries.
  132. */
  133. smp_mb();
  134. if (!list_empty(&fs_info->tree_mod_seq_list)) {
  135. printk(KERN_ERR "btrfs: tree_mod_seq_list not empty when "
  136. "creating a fresh transaction\n");
  137. WARN_ON(1);
  138. }
  139. if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log)) {
  140. printk(KERN_ERR "btrfs: tree_mod_log rb tree not empty when "
  141. "creating a fresh transaction\n");
  142. WARN_ON(1);
  143. }
  144. atomic_set(&fs_info->tree_mod_seq, 0);
  145. spin_lock_init(&cur_trans->commit_lock);
  146. spin_lock_init(&cur_trans->delayed_refs.lock);
  147. INIT_LIST_HEAD(&cur_trans->pending_snapshots);
  148. list_add_tail(&cur_trans->list, &fs_info->trans_list);
  149. extent_io_tree_init(&cur_trans->dirty_pages,
  150. fs_info->btree_inode->i_mapping);
  151. fs_info->generation++;
  152. cur_trans->transid = fs_info->generation;
  153. fs_info->running_transaction = cur_trans;
  154. cur_trans->aborted = 0;
  155. spin_unlock(&fs_info->trans_lock);
  156. return 0;
  157. }
  158. /*
  159. * this does all the record keeping required to make sure that a reference
  160. * counted root is properly recorded in a given transaction. This is required
  161. * to make sure the old root from before we joined the transaction is deleted
  162. * when the transaction commits
  163. */
  164. static int record_root_in_trans(struct btrfs_trans_handle *trans,
  165. struct btrfs_root *root)
  166. {
  167. if (root->ref_cows && root->last_trans < trans->transid) {
  168. WARN_ON(root == root->fs_info->extent_root);
  169. WARN_ON(root->commit_root != root->node);
  170. /*
  171. * see below for in_trans_setup usage rules
  172. * we have the reloc mutex held now, so there
  173. * is only one writer in this function
  174. */
  175. root->in_trans_setup = 1;
  176. /* make sure readers find in_trans_setup before
  177. * they find our root->last_trans update
  178. */
  179. smp_wmb();
  180. spin_lock(&root->fs_info->fs_roots_radix_lock);
  181. if (root->last_trans == trans->transid) {
  182. spin_unlock(&root->fs_info->fs_roots_radix_lock);
  183. return 0;
  184. }
  185. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  186. (unsigned long)root->root_key.objectid,
  187. BTRFS_ROOT_TRANS_TAG);
  188. spin_unlock(&root->fs_info->fs_roots_radix_lock);
  189. root->last_trans = trans->transid;
  190. /* this is pretty tricky. We don't want to
  191. * take the relocation lock in btrfs_record_root_in_trans
  192. * unless we're really doing the first setup for this root in
  193. * this transaction.
  194. *
  195. * Normally we'd use root->last_trans as a flag to decide
  196. * if we want to take the expensive mutex.
  197. *
  198. * But, we have to set root->last_trans before we
  199. * init the relocation root, otherwise, we trip over warnings
  200. * in ctree.c. The solution used here is to flag ourselves
  201. * with root->in_trans_setup. When this is 1, we're still
  202. * fixing up the reloc trees and everyone must wait.
  203. *
  204. * When this is zero, they can trust root->last_trans and fly
  205. * through btrfs_record_root_in_trans without having to take the
  206. * lock. smp_wmb() makes sure that all the writes above are
  207. * done before we pop in the zero below
  208. */
  209. btrfs_init_reloc_root(trans, root);
  210. smp_wmb();
  211. root->in_trans_setup = 0;
  212. }
  213. return 0;
  214. }
  215. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  216. struct btrfs_root *root)
  217. {
  218. if (!root->ref_cows)
  219. return 0;
  220. /*
  221. * see record_root_in_trans for comments about in_trans_setup usage
  222. * and barriers
  223. */
  224. smp_rmb();
  225. if (root->last_trans == trans->transid &&
  226. !root->in_trans_setup)
  227. return 0;
  228. mutex_lock(&root->fs_info->reloc_mutex);
  229. record_root_in_trans(trans, root);
  230. mutex_unlock(&root->fs_info->reloc_mutex);
  231. return 0;
  232. }
  233. /* wait for commit against the current transaction to become unblocked
  234. * when this is done, it is safe to start a new transaction, but the current
  235. * transaction might not be fully on disk.
  236. */
  237. static void wait_current_trans(struct btrfs_root *root)
  238. {
  239. struct btrfs_transaction *cur_trans;
  240. spin_lock(&root->fs_info->trans_lock);
  241. cur_trans = root->fs_info->running_transaction;
  242. if (cur_trans && cur_trans->blocked) {
  243. atomic_inc(&cur_trans->use_count);
  244. spin_unlock(&root->fs_info->trans_lock);
  245. wait_event(root->fs_info->transaction_wait,
  246. !cur_trans->blocked);
  247. put_transaction(cur_trans);
  248. } else {
  249. spin_unlock(&root->fs_info->trans_lock);
  250. }
  251. }
  252. static int may_wait_transaction(struct btrfs_root *root, int type)
  253. {
  254. if (root->fs_info->log_root_recovering)
  255. return 0;
  256. if (type == TRANS_USERSPACE)
  257. return 1;
  258. if (type == TRANS_START &&
  259. !atomic_read(&root->fs_info->open_ioctl_trans))
  260. return 1;
  261. return 0;
  262. }
  263. static struct btrfs_trans_handle *
  264. start_transaction(struct btrfs_root *root, u64 num_items, int type,
  265. enum btrfs_reserve_flush_enum flush)
  266. {
  267. struct btrfs_trans_handle *h;
  268. struct btrfs_transaction *cur_trans;
  269. u64 num_bytes = 0;
  270. int ret;
  271. u64 qgroup_reserved = 0;
  272. if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
  273. return ERR_PTR(-EROFS);
  274. if (current->journal_info) {
  275. WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
  276. h = current->journal_info;
  277. h->use_count++;
  278. WARN_ON(h->use_count > 2);
  279. h->orig_rsv = h->block_rsv;
  280. h->block_rsv = NULL;
  281. goto got_it;
  282. }
  283. /*
  284. * Do the reservation before we join the transaction so we can do all
  285. * the appropriate flushing if need be.
  286. */
  287. if (num_items > 0 && root != root->fs_info->chunk_root) {
  288. if (root->fs_info->quota_enabled &&
  289. is_fstree(root->root_key.objectid)) {
  290. qgroup_reserved = num_items * root->leafsize;
  291. ret = btrfs_qgroup_reserve(root, qgroup_reserved);
  292. if (ret)
  293. return ERR_PTR(ret);
  294. }
  295. num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
  296. ret = btrfs_block_rsv_add(root,
  297. &root->fs_info->trans_block_rsv,
  298. num_bytes, flush);
  299. if (ret)
  300. return ERR_PTR(ret);
  301. }
  302. again:
  303. h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  304. if (!h)
  305. return ERR_PTR(-ENOMEM);
  306. /*
  307. * If we are JOIN_NOLOCK we're already committing a transaction and
  308. * waiting on this guy, so we don't need to do the sb_start_intwrite
  309. * because we're already holding a ref. We need this because we could
  310. * have raced in and did an fsync() on a file which can kick a commit
  311. * and then we deadlock with somebody doing a freeze.
  312. *
  313. * If we are ATTACH, it means we just want to catch the current
  314. * transaction and commit it, so we needn't do sb_start_intwrite().
  315. */
  316. if (type < TRANS_JOIN_NOLOCK)
  317. sb_start_intwrite(root->fs_info->sb);
  318. if (may_wait_transaction(root, type))
  319. wait_current_trans(root);
  320. do {
  321. ret = join_transaction(root, type);
  322. if (ret == -EBUSY)
  323. wait_current_trans(root);
  324. } while (ret == -EBUSY);
  325. if (ret < 0) {
  326. /* We must get the transaction if we are JOIN_NOLOCK. */
  327. BUG_ON(type == TRANS_JOIN_NOLOCK);
  328. if (type < TRANS_JOIN_NOLOCK)
  329. sb_end_intwrite(root->fs_info->sb);
  330. kmem_cache_free(btrfs_trans_handle_cachep, h);
  331. return ERR_PTR(ret);
  332. }
  333. cur_trans = root->fs_info->running_transaction;
  334. h->transid = cur_trans->transid;
  335. h->transaction = cur_trans;
  336. h->blocks_used = 0;
  337. h->bytes_reserved = 0;
  338. h->root = root;
  339. h->delayed_ref_updates = 0;
  340. h->use_count = 1;
  341. h->adding_csums = 0;
  342. h->block_rsv = NULL;
  343. h->orig_rsv = NULL;
  344. h->aborted = 0;
  345. h->qgroup_reserved = qgroup_reserved;
  346. h->delayed_ref_elem.seq = 0;
  347. h->type = type;
  348. INIT_LIST_HEAD(&h->qgroup_ref_list);
  349. INIT_LIST_HEAD(&h->new_bgs);
  350. smp_mb();
  351. if (cur_trans->blocked && may_wait_transaction(root, type)) {
  352. btrfs_commit_transaction(h, root);
  353. goto again;
  354. }
  355. if (num_bytes) {
  356. trace_btrfs_space_reservation(root->fs_info, "transaction",
  357. h->transid, num_bytes, 1);
  358. h->block_rsv = &root->fs_info->trans_block_rsv;
  359. h->bytes_reserved = num_bytes;
  360. }
  361. got_it:
  362. btrfs_record_root_in_trans(h, root);
  363. if (!current->journal_info && type != TRANS_USERSPACE)
  364. current->journal_info = h;
  365. return h;
  366. }
  367. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  368. int num_items)
  369. {
  370. return start_transaction(root, num_items, TRANS_START,
  371. BTRFS_RESERVE_FLUSH_ALL);
  372. }
  373. struct btrfs_trans_handle *btrfs_start_transaction_lflush(
  374. struct btrfs_root *root, int num_items)
  375. {
  376. return start_transaction(root, num_items, TRANS_START,
  377. BTRFS_RESERVE_FLUSH_LIMIT);
  378. }
  379. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
  380. {
  381. return start_transaction(root, 0, TRANS_JOIN, 0);
  382. }
  383. struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
  384. {
  385. return start_transaction(root, 0, TRANS_JOIN_NOLOCK, 0);
  386. }
  387. struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
  388. {
  389. return start_transaction(root, 0, TRANS_USERSPACE, 0);
  390. }
  391. struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
  392. {
  393. return start_transaction(root, 0, TRANS_ATTACH, 0);
  394. }
  395. /* wait for a transaction commit to be fully complete */
  396. static noinline void wait_for_commit(struct btrfs_root *root,
  397. struct btrfs_transaction *commit)
  398. {
  399. wait_event(commit->commit_wait, commit->commit_done);
  400. }
  401. int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
  402. {
  403. struct btrfs_transaction *cur_trans = NULL, *t;
  404. int ret;
  405. ret = 0;
  406. if (transid) {
  407. if (transid <= root->fs_info->last_trans_committed)
  408. goto out;
  409. /* find specified transaction */
  410. spin_lock(&root->fs_info->trans_lock);
  411. list_for_each_entry(t, &root->fs_info->trans_list, list) {
  412. if (t->transid == transid) {
  413. cur_trans = t;
  414. atomic_inc(&cur_trans->use_count);
  415. break;
  416. }
  417. if (t->transid > transid)
  418. break;
  419. }
  420. spin_unlock(&root->fs_info->trans_lock);
  421. ret = -EINVAL;
  422. if (!cur_trans)
  423. goto out; /* bad transid */
  424. } else {
  425. /* find newest transaction that is committing | committed */
  426. spin_lock(&root->fs_info->trans_lock);
  427. list_for_each_entry_reverse(t, &root->fs_info->trans_list,
  428. list) {
  429. if (t->in_commit) {
  430. if (t->commit_done)
  431. break;
  432. cur_trans = t;
  433. atomic_inc(&cur_trans->use_count);
  434. break;
  435. }
  436. }
  437. spin_unlock(&root->fs_info->trans_lock);
  438. if (!cur_trans)
  439. goto out; /* nothing committing|committed */
  440. }
  441. wait_for_commit(root, cur_trans);
  442. put_transaction(cur_trans);
  443. ret = 0;
  444. out:
  445. return ret;
  446. }
  447. void btrfs_throttle(struct btrfs_root *root)
  448. {
  449. if (!atomic_read(&root->fs_info->open_ioctl_trans))
  450. wait_current_trans(root);
  451. }
  452. static int should_end_transaction(struct btrfs_trans_handle *trans,
  453. struct btrfs_root *root)
  454. {
  455. int ret;
  456. ret = btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5);
  457. return ret ? 1 : 0;
  458. }
  459. int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
  460. struct btrfs_root *root)
  461. {
  462. struct btrfs_transaction *cur_trans = trans->transaction;
  463. int updates;
  464. int err;
  465. smp_mb();
  466. if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
  467. return 1;
  468. updates = trans->delayed_ref_updates;
  469. trans->delayed_ref_updates = 0;
  470. if (updates) {
  471. err = btrfs_run_delayed_refs(trans, root, updates);
  472. if (err) /* Error code will also eval true */
  473. return err;
  474. }
  475. return should_end_transaction(trans, root);
  476. }
  477. static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
  478. struct btrfs_root *root, int throttle)
  479. {
  480. struct btrfs_transaction *cur_trans = trans->transaction;
  481. struct btrfs_fs_info *info = root->fs_info;
  482. int count = 0;
  483. int lock = (trans->type != TRANS_JOIN_NOLOCK);
  484. int err = 0;
  485. if (--trans->use_count) {
  486. trans->block_rsv = trans->orig_rsv;
  487. return 0;
  488. }
  489. /*
  490. * do the qgroup accounting as early as possible
  491. */
  492. err = btrfs_delayed_refs_qgroup_accounting(trans, info);
  493. btrfs_trans_release_metadata(trans, root);
  494. trans->block_rsv = NULL;
  495. /*
  496. * the same root has to be passed to start_transaction and
  497. * end_transaction. Subvolume quota depends on this.
  498. */
  499. WARN_ON(trans->root != root);
  500. if (trans->qgroup_reserved) {
  501. btrfs_qgroup_free(root, trans->qgroup_reserved);
  502. trans->qgroup_reserved = 0;
  503. }
  504. if (!list_empty(&trans->new_bgs))
  505. btrfs_create_pending_block_groups(trans, root);
  506. while (count < 2) {
  507. unsigned long cur = trans->delayed_ref_updates;
  508. trans->delayed_ref_updates = 0;
  509. if (cur &&
  510. trans->transaction->delayed_refs.num_heads_ready > 64) {
  511. trans->delayed_ref_updates = 0;
  512. btrfs_run_delayed_refs(trans, root, cur);
  513. } else {
  514. break;
  515. }
  516. count++;
  517. }
  518. btrfs_trans_release_metadata(trans, root);
  519. trans->block_rsv = NULL;
  520. if (!list_empty(&trans->new_bgs))
  521. btrfs_create_pending_block_groups(trans, root);
  522. if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) &&
  523. should_end_transaction(trans, root)) {
  524. trans->transaction->blocked = 1;
  525. smp_wmb();
  526. }
  527. if (lock && cur_trans->blocked && !cur_trans->in_commit) {
  528. if (throttle) {
  529. /*
  530. * We may race with somebody else here so end up having
  531. * to call end_transaction on ourselves again, so inc
  532. * our use_count.
  533. */
  534. trans->use_count++;
  535. return btrfs_commit_transaction(trans, root);
  536. } else {
  537. wake_up_process(info->transaction_kthread);
  538. }
  539. }
  540. if (trans->type < TRANS_JOIN_NOLOCK)
  541. sb_end_intwrite(root->fs_info->sb);
  542. WARN_ON(cur_trans != info->running_transaction);
  543. WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
  544. atomic_dec(&cur_trans->num_writers);
  545. smp_mb();
  546. if (waitqueue_active(&cur_trans->writer_wait))
  547. wake_up(&cur_trans->writer_wait);
  548. put_transaction(cur_trans);
  549. if (current->journal_info == trans)
  550. current->journal_info = NULL;
  551. if (throttle)
  552. btrfs_run_delayed_iputs(root);
  553. if (trans->aborted ||
  554. root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
  555. err = -EIO;
  556. }
  557. assert_qgroups_uptodate(trans);
  558. memset(trans, 0, sizeof(*trans));
  559. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  560. return err;
  561. }
  562. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  563. struct btrfs_root *root)
  564. {
  565. int ret;
  566. ret = __btrfs_end_transaction(trans, root, 0);
  567. if (ret)
  568. return ret;
  569. return 0;
  570. }
  571. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  572. struct btrfs_root *root)
  573. {
  574. int ret;
  575. ret = __btrfs_end_transaction(trans, root, 1);
  576. if (ret)
  577. return ret;
  578. return 0;
  579. }
  580. int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
  581. struct btrfs_root *root)
  582. {
  583. return __btrfs_end_transaction(trans, root, 1);
  584. }
  585. /*
  586. * when btree blocks are allocated, they have some corresponding bits set for
  587. * them in one of two extent_io trees. This is used to make sure all of
  588. * those extents are sent to disk but does not wait on them
  589. */
  590. int btrfs_write_marked_extents(struct btrfs_root *root,
  591. struct extent_io_tree *dirty_pages, int mark)
  592. {
  593. int err = 0;
  594. int werr = 0;
  595. struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
  596. struct extent_state *cached_state = NULL;
  597. u64 start = 0;
  598. u64 end;
  599. while (!find_first_extent_bit(dirty_pages, start, &start, &end,
  600. mark, &cached_state)) {
  601. convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
  602. mark, &cached_state, GFP_NOFS);
  603. cached_state = NULL;
  604. err = filemap_fdatawrite_range(mapping, start, end);
  605. if (err)
  606. werr = err;
  607. cond_resched();
  608. start = end + 1;
  609. }
  610. if (err)
  611. werr = err;
  612. return werr;
  613. }
  614. /*
  615. * when btree blocks are allocated, they have some corresponding bits set for
  616. * them in one of two extent_io trees. This is used to make sure all of
  617. * those extents are on disk for transaction or log commit. We wait
  618. * on all the pages and clear them from the dirty pages state tree
  619. */
  620. int btrfs_wait_marked_extents(struct btrfs_root *root,
  621. struct extent_io_tree *dirty_pages, int mark)
  622. {
  623. int err = 0;
  624. int werr = 0;
  625. struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
  626. struct extent_state *cached_state = NULL;
  627. u64 start = 0;
  628. u64 end;
  629. while (!find_first_extent_bit(dirty_pages, start, &start, &end,
  630. EXTENT_NEED_WAIT, &cached_state)) {
  631. clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
  632. 0, 0, &cached_state, GFP_NOFS);
  633. err = filemap_fdatawait_range(mapping, start, end);
  634. if (err)
  635. werr = err;
  636. cond_resched();
  637. start = end + 1;
  638. }
  639. if (err)
  640. werr = err;
  641. return werr;
  642. }
  643. /*
  644. * when btree blocks are allocated, they have some corresponding bits set for
  645. * them in one of two extent_io trees. This is used to make sure all of
  646. * those extents are on disk for transaction or log commit
  647. */
  648. int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
  649. struct extent_io_tree *dirty_pages, int mark)
  650. {
  651. int ret;
  652. int ret2;
  653. ret = btrfs_write_marked_extents(root, dirty_pages, mark);
  654. ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
  655. if (ret)
  656. return ret;
  657. if (ret2)
  658. return ret2;
  659. return 0;
  660. }
  661. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  662. struct btrfs_root *root)
  663. {
  664. if (!trans || !trans->transaction) {
  665. struct inode *btree_inode;
  666. btree_inode = root->fs_info->btree_inode;
  667. return filemap_write_and_wait(btree_inode->i_mapping);
  668. }
  669. return btrfs_write_and_wait_marked_extents(root,
  670. &trans->transaction->dirty_pages,
  671. EXTENT_DIRTY);
  672. }
  673. /*
  674. * this is used to update the root pointer in the tree of tree roots.
  675. *
  676. * But, in the case of the extent allocation tree, updating the root
  677. * pointer may allocate blocks which may change the root of the extent
  678. * allocation tree.
  679. *
  680. * So, this loops and repeats and makes sure the cowonly root didn't
  681. * change while the root pointer was being updated in the metadata.
  682. */
  683. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  684. struct btrfs_root *root)
  685. {
  686. int ret;
  687. u64 old_root_bytenr;
  688. u64 old_root_used;
  689. struct btrfs_root *tree_root = root->fs_info->tree_root;
  690. old_root_used = btrfs_root_used(&root->root_item);
  691. btrfs_write_dirty_block_groups(trans, root);
  692. while (1) {
  693. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  694. if (old_root_bytenr == root->node->start &&
  695. old_root_used == btrfs_root_used(&root->root_item))
  696. break;
  697. btrfs_set_root_node(&root->root_item, root->node);
  698. ret = btrfs_update_root(trans, tree_root,
  699. &root->root_key,
  700. &root->root_item);
  701. if (ret)
  702. return ret;
  703. old_root_used = btrfs_root_used(&root->root_item);
  704. ret = btrfs_write_dirty_block_groups(trans, root);
  705. if (ret)
  706. return ret;
  707. }
  708. if (root != root->fs_info->extent_root)
  709. switch_commit_root(root);
  710. return 0;
  711. }
  712. /*
  713. * update all the cowonly tree roots on disk
  714. *
  715. * The error handling in this function may not be obvious. Any of the
  716. * failures will cause the file system to go offline. We still need
  717. * to clean up the delayed refs.
  718. */
  719. static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
  720. struct btrfs_root *root)
  721. {
  722. struct btrfs_fs_info *fs_info = root->fs_info;
  723. struct list_head *next;
  724. struct extent_buffer *eb;
  725. int ret;
  726. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  727. if (ret)
  728. return ret;
  729. eb = btrfs_lock_root_node(fs_info->tree_root);
  730. ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
  731. 0, &eb);
  732. btrfs_tree_unlock(eb);
  733. free_extent_buffer(eb);
  734. if (ret)
  735. return ret;
  736. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  737. if (ret)
  738. return ret;
  739. ret = btrfs_run_dev_stats(trans, root->fs_info);
  740. BUG_ON(ret);
  741. ret = btrfs_run_qgroups(trans, root->fs_info);
  742. BUG_ON(ret);
  743. /* run_qgroups might have added some more refs */
  744. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  745. BUG_ON(ret);
  746. while (!list_empty(&fs_info->dirty_cowonly_roots)) {
  747. next = fs_info->dirty_cowonly_roots.next;
  748. list_del_init(next);
  749. root = list_entry(next, struct btrfs_root, dirty_list);
  750. ret = update_cowonly_root(trans, root);
  751. if (ret)
  752. return ret;
  753. }
  754. down_write(&fs_info->extent_commit_sem);
  755. switch_commit_root(fs_info->extent_root);
  756. up_write(&fs_info->extent_commit_sem);
  757. return 0;
  758. }
  759. /*
  760. * dead roots are old snapshots that need to be deleted. This allocates
  761. * a dirty root struct and adds it into the list of dead roots that need to
  762. * be deleted
  763. */
  764. int btrfs_add_dead_root(struct btrfs_root *root)
  765. {
  766. spin_lock(&root->fs_info->trans_lock);
  767. list_add(&root->root_list, &root->fs_info->dead_roots);
  768. spin_unlock(&root->fs_info->trans_lock);
  769. return 0;
  770. }
  771. /*
  772. * update all the cowonly tree roots on disk
  773. */
  774. static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
  775. struct btrfs_root *root)
  776. {
  777. struct btrfs_root *gang[8];
  778. struct btrfs_fs_info *fs_info = root->fs_info;
  779. int i;
  780. int ret;
  781. int err = 0;
  782. spin_lock(&fs_info->fs_roots_radix_lock);
  783. while (1) {
  784. ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
  785. (void **)gang, 0,
  786. ARRAY_SIZE(gang),
  787. BTRFS_ROOT_TRANS_TAG);
  788. if (ret == 0)
  789. break;
  790. for (i = 0; i < ret; i++) {
  791. root = gang[i];
  792. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  793. (unsigned long)root->root_key.objectid,
  794. BTRFS_ROOT_TRANS_TAG);
  795. spin_unlock(&fs_info->fs_roots_radix_lock);
  796. btrfs_free_log(trans, root);
  797. btrfs_update_reloc_root(trans, root);
  798. btrfs_orphan_commit_root(trans, root);
  799. btrfs_save_ino_cache(root, trans);
  800. /* see comments in should_cow_block() */
  801. root->force_cow = 0;
  802. smp_wmb();
  803. if (root->commit_root != root->node) {
  804. mutex_lock(&root->fs_commit_mutex);
  805. switch_commit_root(root);
  806. btrfs_unpin_free_ino(root);
  807. mutex_unlock(&root->fs_commit_mutex);
  808. btrfs_set_root_node(&root->root_item,
  809. root->node);
  810. }
  811. err = btrfs_update_root(trans, fs_info->tree_root,
  812. &root->root_key,
  813. &root->root_item);
  814. spin_lock(&fs_info->fs_roots_radix_lock);
  815. if (err)
  816. break;
  817. }
  818. }
  819. spin_unlock(&fs_info->fs_roots_radix_lock);
  820. return err;
  821. }
  822. /*
  823. * defrag a given btree. If cacheonly == 1, this won't read from the disk,
  824. * otherwise every leaf in the btree is read and defragged.
  825. */
  826. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  827. {
  828. struct btrfs_fs_info *info = root->fs_info;
  829. struct btrfs_trans_handle *trans;
  830. int ret;
  831. unsigned long nr;
  832. if (xchg(&root->defrag_running, 1))
  833. return 0;
  834. while (1) {
  835. trans = btrfs_start_transaction(root, 0);
  836. if (IS_ERR(trans))
  837. return PTR_ERR(trans);
  838. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  839. nr = trans->blocks_used;
  840. btrfs_end_transaction(trans, root);
  841. btrfs_btree_balance_dirty(info->tree_root, nr);
  842. cond_resched();
  843. if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
  844. break;
  845. }
  846. root->defrag_running = 0;
  847. return ret;
  848. }
  849. /*
  850. * new snapshots need to be created at a very specific time in the
  851. * transaction commit. This does the actual creation
  852. */
  853. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  854. struct btrfs_fs_info *fs_info,
  855. struct btrfs_pending_snapshot *pending)
  856. {
  857. struct btrfs_key key;
  858. struct btrfs_root_item *new_root_item;
  859. struct btrfs_root *tree_root = fs_info->tree_root;
  860. struct btrfs_root *root = pending->root;
  861. struct btrfs_root *parent_root;
  862. struct btrfs_block_rsv *rsv;
  863. struct inode *parent_inode;
  864. struct btrfs_path *path;
  865. struct btrfs_dir_item *dir_item;
  866. struct dentry *parent;
  867. struct dentry *dentry;
  868. struct extent_buffer *tmp;
  869. struct extent_buffer *old;
  870. struct timespec cur_time = CURRENT_TIME;
  871. int ret;
  872. u64 to_reserve = 0;
  873. u64 index = 0;
  874. u64 objectid;
  875. u64 root_flags;
  876. uuid_le new_uuid;
  877. path = btrfs_alloc_path();
  878. if (!path) {
  879. ret = pending->error = -ENOMEM;
  880. goto path_alloc_fail;
  881. }
  882. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  883. if (!new_root_item) {
  884. ret = pending->error = -ENOMEM;
  885. goto root_item_alloc_fail;
  886. }
  887. ret = btrfs_find_free_objectid(tree_root, &objectid);
  888. if (ret) {
  889. pending->error = ret;
  890. goto no_free_objectid;
  891. }
  892. btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
  893. if (to_reserve > 0) {
  894. ret = btrfs_block_rsv_add(root, &pending->block_rsv,
  895. to_reserve,
  896. BTRFS_RESERVE_NO_FLUSH);
  897. if (ret) {
  898. pending->error = ret;
  899. goto no_free_objectid;
  900. }
  901. }
  902. ret = btrfs_qgroup_inherit(trans, fs_info, root->root_key.objectid,
  903. objectid, pending->inherit);
  904. if (ret) {
  905. pending->error = ret;
  906. goto no_free_objectid;
  907. }
  908. key.objectid = objectid;
  909. key.offset = (u64)-1;
  910. key.type = BTRFS_ROOT_ITEM_KEY;
  911. rsv = trans->block_rsv;
  912. trans->block_rsv = &pending->block_rsv;
  913. dentry = pending->dentry;
  914. parent = dget_parent(dentry);
  915. parent_inode = parent->d_inode;
  916. parent_root = BTRFS_I(parent_inode)->root;
  917. record_root_in_trans(trans, parent_root);
  918. /*
  919. * insert the directory item
  920. */
  921. ret = btrfs_set_inode_index(parent_inode, &index);
  922. BUG_ON(ret); /* -ENOMEM */
  923. /* check if there is a file/dir which has the same name. */
  924. dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
  925. btrfs_ino(parent_inode),
  926. dentry->d_name.name,
  927. dentry->d_name.len, 0);
  928. if (dir_item != NULL && !IS_ERR(dir_item)) {
  929. pending->error = -EEXIST;
  930. goto fail;
  931. } else if (IS_ERR(dir_item)) {
  932. ret = PTR_ERR(dir_item);
  933. btrfs_abort_transaction(trans, root, ret);
  934. goto fail;
  935. }
  936. btrfs_release_path(path);
  937. /*
  938. * pull in the delayed directory update
  939. * and the delayed inode item
  940. * otherwise we corrupt the FS during
  941. * snapshot
  942. */
  943. ret = btrfs_run_delayed_items(trans, root);
  944. if (ret) { /* Transaction aborted */
  945. btrfs_abort_transaction(trans, root, ret);
  946. goto fail;
  947. }
  948. record_root_in_trans(trans, root);
  949. btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  950. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  951. btrfs_check_and_init_root_item(new_root_item);
  952. root_flags = btrfs_root_flags(new_root_item);
  953. if (pending->readonly)
  954. root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
  955. else
  956. root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
  957. btrfs_set_root_flags(new_root_item, root_flags);
  958. btrfs_set_root_generation_v2(new_root_item,
  959. trans->transid);
  960. uuid_le_gen(&new_uuid);
  961. memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
  962. memcpy(new_root_item->parent_uuid, root->root_item.uuid,
  963. BTRFS_UUID_SIZE);
  964. new_root_item->otime.sec = cpu_to_le64(cur_time.tv_sec);
  965. new_root_item->otime.nsec = cpu_to_le32(cur_time.tv_nsec);
  966. btrfs_set_root_otransid(new_root_item, trans->transid);
  967. memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
  968. memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
  969. btrfs_set_root_stransid(new_root_item, 0);
  970. btrfs_set_root_rtransid(new_root_item, 0);
  971. old = btrfs_lock_root_node(root);
  972. ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
  973. if (ret) {
  974. btrfs_tree_unlock(old);
  975. free_extent_buffer(old);
  976. btrfs_abort_transaction(trans, root, ret);
  977. goto fail;
  978. }
  979. btrfs_set_lock_blocking(old);
  980. ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
  981. /* clean up in any case */
  982. btrfs_tree_unlock(old);
  983. free_extent_buffer(old);
  984. if (ret) {
  985. btrfs_abort_transaction(trans, root, ret);
  986. goto fail;
  987. }
  988. /* see comments in should_cow_block() */
  989. root->force_cow = 1;
  990. smp_wmb();
  991. btrfs_set_root_node(new_root_item, tmp);
  992. /* record when the snapshot was created in key.offset */
  993. key.offset = trans->transid;
  994. ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
  995. btrfs_tree_unlock(tmp);
  996. free_extent_buffer(tmp);
  997. if (ret) {
  998. btrfs_abort_transaction(trans, root, ret);
  999. goto fail;
  1000. }
  1001. /*
  1002. * insert root back/forward references
  1003. */
  1004. ret = btrfs_add_root_ref(trans, tree_root, objectid,
  1005. parent_root->root_key.objectid,
  1006. btrfs_ino(parent_inode), index,
  1007. dentry->d_name.name, dentry->d_name.len);
  1008. if (ret) {
  1009. btrfs_abort_transaction(trans, root, ret);
  1010. goto fail;
  1011. }
  1012. key.offset = (u64)-1;
  1013. pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
  1014. if (IS_ERR(pending->snap)) {
  1015. ret = PTR_ERR(pending->snap);
  1016. btrfs_abort_transaction(trans, root, ret);
  1017. goto fail;
  1018. }
  1019. ret = btrfs_reloc_post_snapshot(trans, pending);
  1020. if (ret) {
  1021. btrfs_abort_transaction(trans, root, ret);
  1022. goto fail;
  1023. }
  1024. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  1025. if (ret) {
  1026. btrfs_abort_transaction(trans, root, ret);
  1027. goto fail;
  1028. }
  1029. ret = btrfs_insert_dir_item(trans, parent_root,
  1030. dentry->d_name.name, dentry->d_name.len,
  1031. parent_inode, &key,
  1032. BTRFS_FT_DIR, index);
  1033. /* We have check then name at the beginning, so it is impossible. */
  1034. BUG_ON(ret == -EEXIST);
  1035. if (ret) {
  1036. btrfs_abort_transaction(trans, root, ret);
  1037. goto fail;
  1038. }
  1039. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  1040. dentry->d_name.len * 2);
  1041. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  1042. ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
  1043. if (ret)
  1044. btrfs_abort_transaction(trans, root, ret);
  1045. fail:
  1046. dput(parent);
  1047. trans->block_rsv = rsv;
  1048. no_free_objectid:
  1049. kfree(new_root_item);
  1050. root_item_alloc_fail:
  1051. btrfs_free_path(path);
  1052. path_alloc_fail:
  1053. btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
  1054. return ret;
  1055. }
  1056. /*
  1057. * create all the snapshots we've scheduled for creation
  1058. */
  1059. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  1060. struct btrfs_fs_info *fs_info)
  1061. {
  1062. struct btrfs_pending_snapshot *pending;
  1063. struct list_head *head = &trans->transaction->pending_snapshots;
  1064. list_for_each_entry(pending, head, list)
  1065. create_pending_snapshot(trans, fs_info, pending);
  1066. return 0;
  1067. }
  1068. static void update_super_roots(struct btrfs_root *root)
  1069. {
  1070. struct btrfs_root_item *root_item;
  1071. struct btrfs_super_block *super;
  1072. super = root->fs_info->super_copy;
  1073. root_item = &root->fs_info->chunk_root->root_item;
  1074. super->chunk_root = root_item->bytenr;
  1075. super->chunk_root_generation = root_item->generation;
  1076. super->chunk_root_level = root_item->level;
  1077. root_item = &root->fs_info->tree_root->root_item;
  1078. super->root = root_item->bytenr;
  1079. super->generation = root_item->generation;
  1080. super->root_level = root_item->level;
  1081. if (btrfs_test_opt(root, SPACE_CACHE))
  1082. super->cache_generation = root_item->generation;
  1083. }
  1084. int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
  1085. {
  1086. int ret = 0;
  1087. spin_lock(&info->trans_lock);
  1088. if (info->running_transaction)
  1089. ret = info->running_transaction->in_commit;
  1090. spin_unlock(&info->trans_lock);
  1091. return ret;
  1092. }
  1093. int btrfs_transaction_blocked(struct btrfs_fs_info *info)
  1094. {
  1095. int ret = 0;
  1096. spin_lock(&info->trans_lock);
  1097. if (info->running_transaction)
  1098. ret = info->running_transaction->blocked;
  1099. spin_unlock(&info->trans_lock);
  1100. return ret;
  1101. }
  1102. /*
  1103. * wait for the current transaction commit to start and block subsequent
  1104. * transaction joins
  1105. */
  1106. static void wait_current_trans_commit_start(struct btrfs_root *root,
  1107. struct btrfs_transaction *trans)
  1108. {
  1109. wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit);
  1110. }
  1111. /*
  1112. * wait for the current transaction to start and then become unblocked.
  1113. * caller holds ref.
  1114. */
  1115. static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
  1116. struct btrfs_transaction *trans)
  1117. {
  1118. wait_event(root->fs_info->transaction_wait,
  1119. trans->commit_done || (trans->in_commit && !trans->blocked));
  1120. }
  1121. /*
  1122. * commit transactions asynchronously. once btrfs_commit_transaction_async
  1123. * returns, any subsequent transaction will not be allowed to join.
  1124. */
  1125. struct btrfs_async_commit {
  1126. struct btrfs_trans_handle *newtrans;
  1127. struct btrfs_root *root;
  1128. struct delayed_work work;
  1129. };
  1130. static void do_async_commit(struct work_struct *work)
  1131. {
  1132. struct btrfs_async_commit *ac =
  1133. container_of(work, struct btrfs_async_commit, work.work);
  1134. /*
  1135. * We've got freeze protection passed with the transaction.
  1136. * Tell lockdep about it.
  1137. */
  1138. rwsem_acquire_read(
  1139. &ac->root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
  1140. 0, 1, _THIS_IP_);
  1141. current->journal_info = ac->newtrans;
  1142. btrfs_commit_transaction(ac->newtrans, ac->root);
  1143. kfree(ac);
  1144. }
  1145. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  1146. struct btrfs_root *root,
  1147. int wait_for_unblock)
  1148. {
  1149. struct btrfs_async_commit *ac;
  1150. struct btrfs_transaction *cur_trans;
  1151. ac = kmalloc(sizeof(*ac), GFP_NOFS);
  1152. if (!ac)
  1153. return -ENOMEM;
  1154. INIT_DELAYED_WORK(&ac->work, do_async_commit);
  1155. ac->root = root;
  1156. ac->newtrans = btrfs_join_transaction(root);
  1157. if (IS_ERR(ac->newtrans)) {
  1158. int err = PTR_ERR(ac->newtrans);
  1159. kfree(ac);
  1160. return err;
  1161. }
  1162. /* take transaction reference */
  1163. cur_trans = trans->transaction;
  1164. atomic_inc(&cur_trans->use_count);
  1165. btrfs_end_transaction(trans, root);
  1166. /*
  1167. * Tell lockdep we've released the freeze rwsem, since the
  1168. * async commit thread will be the one to unlock it.
  1169. */
  1170. rwsem_release(&root->fs_info->sb->s_writers.lock_map[SB_FREEZE_FS-1],
  1171. 1, _THIS_IP_);
  1172. schedule_delayed_work(&ac->work, 0);
  1173. /* wait for transaction to start and unblock */
  1174. if (wait_for_unblock)
  1175. wait_current_trans_commit_start_and_unblock(root, cur_trans);
  1176. else
  1177. wait_current_trans_commit_start(root, cur_trans);
  1178. if (current->journal_info == trans)
  1179. current->journal_info = NULL;
  1180. put_transaction(cur_trans);
  1181. return 0;
  1182. }
  1183. static void cleanup_transaction(struct btrfs_trans_handle *trans,
  1184. struct btrfs_root *root, int err)
  1185. {
  1186. struct btrfs_transaction *cur_trans = trans->transaction;
  1187. WARN_ON(trans->use_count > 1);
  1188. btrfs_abort_transaction(trans, root, err);
  1189. spin_lock(&root->fs_info->trans_lock);
  1190. list_del_init(&cur_trans->list);
  1191. if (cur_trans == root->fs_info->running_transaction) {
  1192. root->fs_info->running_transaction = NULL;
  1193. root->fs_info->trans_no_join = 0;
  1194. }
  1195. spin_unlock(&root->fs_info->trans_lock);
  1196. btrfs_cleanup_one_transaction(trans->transaction, root);
  1197. put_transaction(cur_trans);
  1198. put_transaction(cur_trans);
  1199. trace_btrfs_transaction_commit(root);
  1200. btrfs_scrub_continue(root);
  1201. if (current->journal_info == trans)
  1202. current->journal_info = NULL;
  1203. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1204. }
  1205. static int btrfs_flush_all_pending_stuffs(struct btrfs_trans_handle *trans,
  1206. struct btrfs_root *root)
  1207. {
  1208. int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
  1209. int snap_pending = 0;
  1210. int ret;
  1211. if (!flush_on_commit) {
  1212. spin_lock(&root->fs_info->trans_lock);
  1213. if (!list_empty(&trans->transaction->pending_snapshots))
  1214. snap_pending = 1;
  1215. spin_unlock(&root->fs_info->trans_lock);
  1216. }
  1217. if (flush_on_commit || snap_pending) {
  1218. btrfs_start_delalloc_inodes(root, 1);
  1219. btrfs_wait_ordered_extents(root, 1);
  1220. }
  1221. ret = btrfs_run_delayed_items(trans, root);
  1222. if (ret)
  1223. return ret;
  1224. /*
  1225. * running the delayed items may have added new refs. account
  1226. * them now so that they hinder processing of more delayed refs
  1227. * as little as possible.
  1228. */
  1229. btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
  1230. /*
  1231. * rename don't use btrfs_join_transaction, so, once we
  1232. * set the transaction to blocked above, we aren't going
  1233. * to get any new ordered operations. We can safely run
  1234. * it here and no for sure that nothing new will be added
  1235. * to the list
  1236. */
  1237. btrfs_run_ordered_operations(root, 1);
  1238. return 0;
  1239. }
  1240. /*
  1241. * btrfs_transaction state sequence:
  1242. * in_commit = 0, blocked = 0 (initial)
  1243. * in_commit = 1, blocked = 1
  1244. * blocked = 0
  1245. * commit_done = 1
  1246. */
  1247. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  1248. struct btrfs_root *root)
  1249. {
  1250. unsigned long joined = 0;
  1251. struct btrfs_transaction *cur_trans = trans->transaction;
  1252. struct btrfs_transaction *prev_trans = NULL;
  1253. DEFINE_WAIT(wait);
  1254. int ret;
  1255. int should_grow = 0;
  1256. unsigned long now = get_seconds();
  1257. ret = btrfs_run_ordered_operations(root, 0);
  1258. if (ret) {
  1259. btrfs_abort_transaction(trans, root, ret);
  1260. goto cleanup_transaction;
  1261. }
  1262. if (cur_trans->aborted) {
  1263. ret = cur_trans->aborted;
  1264. goto cleanup_transaction;
  1265. }
  1266. /* make a pass through all the delayed refs we have so far
  1267. * any runnings procs may add more while we are here
  1268. */
  1269. ret = btrfs_run_delayed_refs(trans, root, 0);
  1270. if (ret)
  1271. goto cleanup_transaction;
  1272. btrfs_trans_release_metadata(trans, root);
  1273. trans->block_rsv = NULL;
  1274. cur_trans = trans->transaction;
  1275. /*
  1276. * set the flushing flag so procs in this transaction have to
  1277. * start sending their work down.
  1278. */
  1279. cur_trans->delayed_refs.flushing = 1;
  1280. if (!list_empty(&trans->new_bgs))
  1281. btrfs_create_pending_block_groups(trans, root);
  1282. ret = btrfs_run_delayed_refs(trans, root, 0);
  1283. if (ret)
  1284. goto cleanup_transaction;
  1285. spin_lock(&cur_trans->commit_lock);
  1286. if (cur_trans->in_commit) {
  1287. spin_unlock(&cur_trans->commit_lock);
  1288. atomic_inc(&cur_trans->use_count);
  1289. ret = btrfs_end_transaction(trans, root);
  1290. wait_for_commit(root, cur_trans);
  1291. put_transaction(cur_trans);
  1292. return ret;
  1293. }
  1294. trans->transaction->in_commit = 1;
  1295. trans->transaction->blocked = 1;
  1296. spin_unlock(&cur_trans->commit_lock);
  1297. wake_up(&root->fs_info->transaction_blocked_wait);
  1298. spin_lock(&root->fs_info->trans_lock);
  1299. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  1300. prev_trans = list_entry(cur_trans->list.prev,
  1301. struct btrfs_transaction, list);
  1302. if (!prev_trans->commit_done) {
  1303. atomic_inc(&prev_trans->use_count);
  1304. spin_unlock(&root->fs_info->trans_lock);
  1305. wait_for_commit(root, prev_trans);
  1306. put_transaction(prev_trans);
  1307. } else {
  1308. spin_unlock(&root->fs_info->trans_lock);
  1309. }
  1310. } else {
  1311. spin_unlock(&root->fs_info->trans_lock);
  1312. }
  1313. if (!btrfs_test_opt(root, SSD) &&
  1314. (now < cur_trans->start_time || now - cur_trans->start_time < 1))
  1315. should_grow = 1;
  1316. do {
  1317. joined = cur_trans->num_joined;
  1318. WARN_ON(cur_trans != trans->transaction);
  1319. ret = btrfs_flush_all_pending_stuffs(trans, root);
  1320. if (ret)
  1321. goto cleanup_transaction;
  1322. prepare_to_wait(&cur_trans->writer_wait, &wait,
  1323. TASK_UNINTERRUPTIBLE);
  1324. if (atomic_read(&cur_trans->num_writers) > 1)
  1325. schedule_timeout(MAX_SCHEDULE_TIMEOUT);
  1326. else if (should_grow)
  1327. schedule_timeout(1);
  1328. finish_wait(&cur_trans->writer_wait, &wait);
  1329. } while (atomic_read(&cur_trans->num_writers) > 1 ||
  1330. (should_grow && cur_trans->num_joined != joined));
  1331. ret = btrfs_flush_all_pending_stuffs(trans, root);
  1332. if (ret)
  1333. goto cleanup_transaction;
  1334. /*
  1335. * Ok now we need to make sure to block out any other joins while we
  1336. * commit the transaction. We could have started a join before setting
  1337. * no_join so make sure to wait for num_writers to == 1 again.
  1338. */
  1339. spin_lock(&root->fs_info->trans_lock);
  1340. root->fs_info->trans_no_join = 1;
  1341. spin_unlock(&root->fs_info->trans_lock);
  1342. wait_event(cur_trans->writer_wait,
  1343. atomic_read(&cur_trans->num_writers) == 1);
  1344. /*
  1345. * the reloc mutex makes sure that we stop
  1346. * the balancing code from coming in and moving
  1347. * extents around in the middle of the commit
  1348. */
  1349. mutex_lock(&root->fs_info->reloc_mutex);
  1350. /*
  1351. * We needn't worry about the delayed items because we will
  1352. * deal with them in create_pending_snapshot(), which is the
  1353. * core function of the snapshot creation.
  1354. */
  1355. ret = create_pending_snapshots(trans, root->fs_info);
  1356. if (ret) {
  1357. mutex_unlock(&root->fs_info->reloc_mutex);
  1358. goto cleanup_transaction;
  1359. }
  1360. /*
  1361. * We insert the dir indexes of the snapshots and update the inode
  1362. * of the snapshots' parents after the snapshot creation, so there
  1363. * are some delayed items which are not dealt with. Now deal with
  1364. * them.
  1365. *
  1366. * We needn't worry that this operation will corrupt the snapshots,
  1367. * because all the tree which are snapshoted will be forced to COW
  1368. * the nodes and leaves.
  1369. */
  1370. ret = btrfs_run_delayed_items(trans, root);
  1371. if (ret) {
  1372. mutex_unlock(&root->fs_info->reloc_mutex);
  1373. goto cleanup_transaction;
  1374. }
  1375. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  1376. if (ret) {
  1377. mutex_unlock(&root->fs_info->reloc_mutex);
  1378. goto cleanup_transaction;
  1379. }
  1380. /*
  1381. * make sure none of the code above managed to slip in a
  1382. * delayed item
  1383. */
  1384. btrfs_assert_delayed_root_empty(root);
  1385. WARN_ON(cur_trans != trans->transaction);
  1386. btrfs_scrub_pause(root);
  1387. /* btrfs_commit_tree_roots is responsible for getting the
  1388. * various roots consistent with each other. Every pointer
  1389. * in the tree of tree roots has to point to the most up to date
  1390. * root for every subvolume and other tree. So, we have to keep
  1391. * the tree logging code from jumping in and changing any
  1392. * of the trees.
  1393. *
  1394. * At this point in the commit, there can't be any tree-log
  1395. * writers, but a little lower down we drop the trans mutex
  1396. * and let new people in. By holding the tree_log_mutex
  1397. * from now until after the super is written, we avoid races
  1398. * with the tree-log code.
  1399. */
  1400. mutex_lock(&root->fs_info->tree_log_mutex);
  1401. ret = commit_fs_roots(trans, root);
  1402. if (ret) {
  1403. mutex_unlock(&root->fs_info->tree_log_mutex);
  1404. mutex_unlock(&root->fs_info->reloc_mutex);
  1405. goto cleanup_transaction;
  1406. }
  1407. /* commit_fs_roots gets rid of all the tree log roots, it is now
  1408. * safe to free the root of tree log roots
  1409. */
  1410. btrfs_free_log_root_tree(trans, root->fs_info);
  1411. ret = commit_cowonly_roots(trans, root);
  1412. if (ret) {
  1413. mutex_unlock(&root->fs_info->tree_log_mutex);
  1414. mutex_unlock(&root->fs_info->reloc_mutex);
  1415. goto cleanup_transaction;
  1416. }
  1417. btrfs_prepare_extent_commit(trans, root);
  1418. cur_trans = root->fs_info->running_transaction;
  1419. btrfs_set_root_node(&root->fs_info->tree_root->root_item,
  1420. root->fs_info->tree_root->node);
  1421. switch_commit_root(root->fs_info->tree_root);
  1422. btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
  1423. root->fs_info->chunk_root->node);
  1424. switch_commit_root(root->fs_info->chunk_root);
  1425. assert_qgroups_uptodate(trans);
  1426. update_super_roots(root);
  1427. if (!root->fs_info->log_root_recovering) {
  1428. btrfs_set_super_log_root(root->fs_info->super_copy, 0);
  1429. btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
  1430. }
  1431. memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy,
  1432. sizeof(*root->fs_info->super_copy));
  1433. trans->transaction->blocked = 0;
  1434. spin_lock(&root->fs_info->trans_lock);
  1435. root->fs_info->running_transaction = NULL;
  1436. root->fs_info->trans_no_join = 0;
  1437. spin_unlock(&root->fs_info->trans_lock);
  1438. mutex_unlock(&root->fs_info->reloc_mutex);
  1439. wake_up(&root->fs_info->transaction_wait);
  1440. ret = btrfs_write_and_wait_transaction(trans, root);
  1441. if (ret) {
  1442. btrfs_error(root->fs_info, ret,
  1443. "Error while writing out transaction.");
  1444. mutex_unlock(&root->fs_info->tree_log_mutex);
  1445. goto cleanup_transaction;
  1446. }
  1447. ret = write_ctree_super(trans, root, 0);
  1448. if (ret) {
  1449. mutex_unlock(&root->fs_info->tree_log_mutex);
  1450. goto cleanup_transaction;
  1451. }
  1452. /*
  1453. * the super is written, we can safely allow the tree-loggers
  1454. * to go about their business
  1455. */
  1456. mutex_unlock(&root->fs_info->tree_log_mutex);
  1457. btrfs_finish_extent_commit(trans, root);
  1458. cur_trans->commit_done = 1;
  1459. root->fs_info->last_trans_committed = cur_trans->transid;
  1460. wake_up(&cur_trans->commit_wait);
  1461. spin_lock(&root->fs_info->trans_lock);
  1462. list_del_init(&cur_trans->list);
  1463. spin_unlock(&root->fs_info->trans_lock);
  1464. put_transaction(cur_trans);
  1465. put_transaction(cur_trans);
  1466. if (trans->type < TRANS_JOIN_NOLOCK)
  1467. sb_end_intwrite(root->fs_info->sb);
  1468. trace_btrfs_transaction_commit(root);
  1469. btrfs_scrub_continue(root);
  1470. if (current->journal_info == trans)
  1471. current->journal_info = NULL;
  1472. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1473. if (current != root->fs_info->transaction_kthread)
  1474. btrfs_run_delayed_iputs(root);
  1475. return ret;
  1476. cleanup_transaction:
  1477. btrfs_trans_release_metadata(trans, root);
  1478. trans->block_rsv = NULL;
  1479. btrfs_printk(root->fs_info, "Skipping commit of aborted transaction.\n");
  1480. // WARN_ON(1);
  1481. if (current->journal_info == trans)
  1482. current->journal_info = NULL;
  1483. cleanup_transaction(trans, root, ret);
  1484. return ret;
  1485. }
  1486. /*
  1487. * interface function to delete all the snapshots we have scheduled for deletion
  1488. */
  1489. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  1490. {
  1491. LIST_HEAD(list);
  1492. struct btrfs_fs_info *fs_info = root->fs_info;
  1493. spin_lock(&fs_info->trans_lock);
  1494. list_splice_init(&fs_info->dead_roots, &list);
  1495. spin_unlock(&fs_info->trans_lock);
  1496. while (!list_empty(&list)) {
  1497. int ret;
  1498. root = list_entry(list.next, struct btrfs_root, root_list);
  1499. list_del(&root->root_list);
  1500. btrfs_kill_all_delayed_nodes(root);
  1501. if (btrfs_header_backref_rev(root->node) <
  1502. BTRFS_MIXED_BACKREF_REV)
  1503. ret = btrfs_drop_snapshot(root, NULL, 0, 0);
  1504. else
  1505. ret =btrfs_drop_snapshot(root, NULL, 1, 0);
  1506. BUG_ON(ret < 0);
  1507. }
  1508. return 0;
  1509. }