transaction.c 54 KB

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