transaction.c 45 KB

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