transaction.c 42 KB

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