transaction.c 45 KB

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