transaction.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #include <linux/fs.h>
  6. #include <linux/slab.h>
  7. #include <linux/sched.h>
  8. #include <linux/writeback.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/uuid.h>
  12. #include "ctree.h"
  13. #include "disk-io.h"
  14. #include "transaction.h"
  15. #include "locking.h"
  16. #include "tree-log.h"
  17. #include "inode-map.h"
  18. #include "volumes.h"
  19. #include "dev-replace.h"
  20. #include "qgroup.h"
  21. #define BTRFS_ROOT_TRANS_TAG 0
  22. static const unsigned int btrfs_blocked_trans_types[TRANS_STATE_MAX] = {
  23. [TRANS_STATE_RUNNING] = 0U,
  24. [TRANS_STATE_BLOCKED] = __TRANS_START,
  25. [TRANS_STATE_COMMIT_START] = (__TRANS_START | __TRANS_ATTACH),
  26. [TRANS_STATE_COMMIT_DOING] = (__TRANS_START |
  27. __TRANS_ATTACH |
  28. __TRANS_JOIN |
  29. __TRANS_JOIN_NOSTART),
  30. [TRANS_STATE_UNBLOCKED] = (__TRANS_START |
  31. __TRANS_ATTACH |
  32. __TRANS_JOIN |
  33. __TRANS_JOIN_NOLOCK |
  34. __TRANS_JOIN_NOSTART),
  35. [TRANS_STATE_COMPLETED] = (__TRANS_START |
  36. __TRANS_ATTACH |
  37. __TRANS_JOIN |
  38. __TRANS_JOIN_NOLOCK |
  39. __TRANS_JOIN_NOSTART),
  40. };
  41. void btrfs_put_transaction(struct btrfs_transaction *transaction)
  42. {
  43. WARN_ON(refcount_read(&transaction->use_count) == 0);
  44. if (refcount_dec_and_test(&transaction->use_count)) {
  45. BUG_ON(!list_empty(&transaction->list));
  46. WARN_ON(!RB_EMPTY_ROOT(&transaction->delayed_refs.href_root));
  47. if (transaction->delayed_refs.pending_csums)
  48. btrfs_err(transaction->fs_info,
  49. "pending csums is %llu",
  50. transaction->delayed_refs.pending_csums);
  51. while (!list_empty(&transaction->pending_chunks)) {
  52. struct extent_map *em;
  53. em = list_first_entry(&transaction->pending_chunks,
  54. struct extent_map, list);
  55. list_del_init(&em->list);
  56. free_extent_map(em);
  57. }
  58. /*
  59. * If any block groups are found in ->deleted_bgs then it's
  60. * because the transaction was aborted and a commit did not
  61. * happen (things failed before writing the new superblock
  62. * and calling btrfs_finish_extent_commit()), so we can not
  63. * discard the physical locations of the block groups.
  64. */
  65. while (!list_empty(&transaction->deleted_bgs)) {
  66. struct btrfs_block_group_cache *cache;
  67. cache = list_first_entry(&transaction->deleted_bgs,
  68. struct btrfs_block_group_cache,
  69. bg_list);
  70. list_del_init(&cache->bg_list);
  71. btrfs_put_block_group_trimming(cache);
  72. btrfs_put_block_group(cache);
  73. }
  74. kfree(transaction);
  75. }
  76. }
  77. static void clear_btree_io_tree(struct extent_io_tree *tree)
  78. {
  79. spin_lock(&tree->lock);
  80. /*
  81. * Do a single barrier for the waitqueue_active check here, the state
  82. * of the waitqueue should not change once clear_btree_io_tree is
  83. * called.
  84. */
  85. smp_mb();
  86. while (!RB_EMPTY_ROOT(&tree->state)) {
  87. struct rb_node *node;
  88. struct extent_state *state;
  89. node = rb_first(&tree->state);
  90. state = rb_entry(node, struct extent_state, rb_node);
  91. rb_erase(&state->rb_node, &tree->state);
  92. RB_CLEAR_NODE(&state->rb_node);
  93. /*
  94. * btree io trees aren't supposed to have tasks waiting for
  95. * changes in the flags of extent states ever.
  96. */
  97. ASSERT(!waitqueue_active(&state->wq));
  98. free_extent_state(state);
  99. cond_resched_lock(&tree->lock);
  100. }
  101. spin_unlock(&tree->lock);
  102. }
  103. static noinline void switch_commit_roots(struct btrfs_transaction *trans)
  104. {
  105. struct btrfs_fs_info *fs_info = trans->fs_info;
  106. struct btrfs_root *root, *tmp;
  107. down_write(&fs_info->commit_root_sem);
  108. list_for_each_entry_safe(root, tmp, &trans->switch_commits,
  109. dirty_list) {
  110. list_del_init(&root->dirty_list);
  111. free_extent_buffer(root->commit_root);
  112. root->commit_root = btrfs_root_node(root);
  113. if (is_fstree(root->objectid))
  114. btrfs_unpin_free_ino(root);
  115. clear_btree_io_tree(&root->dirty_log_pages);
  116. }
  117. /* We can free old roots now. */
  118. spin_lock(&trans->dropped_roots_lock);
  119. while (!list_empty(&trans->dropped_roots)) {
  120. root = list_first_entry(&trans->dropped_roots,
  121. struct btrfs_root, root_list);
  122. list_del_init(&root->root_list);
  123. spin_unlock(&trans->dropped_roots_lock);
  124. btrfs_drop_and_free_fs_root(fs_info, root);
  125. spin_lock(&trans->dropped_roots_lock);
  126. }
  127. spin_unlock(&trans->dropped_roots_lock);
  128. up_write(&fs_info->commit_root_sem);
  129. }
  130. static inline void extwriter_counter_inc(struct btrfs_transaction *trans,
  131. unsigned int type)
  132. {
  133. if (type & TRANS_EXTWRITERS)
  134. atomic_inc(&trans->num_extwriters);
  135. }
  136. static inline void extwriter_counter_dec(struct btrfs_transaction *trans,
  137. unsigned int type)
  138. {
  139. if (type & TRANS_EXTWRITERS)
  140. atomic_dec(&trans->num_extwriters);
  141. }
  142. static inline void extwriter_counter_init(struct btrfs_transaction *trans,
  143. unsigned int type)
  144. {
  145. atomic_set(&trans->num_extwriters, ((type & TRANS_EXTWRITERS) ? 1 : 0));
  146. }
  147. static inline int extwriter_counter_read(struct btrfs_transaction *trans)
  148. {
  149. return atomic_read(&trans->num_extwriters);
  150. }
  151. /*
  152. * either allocate a new transaction or hop into the existing one
  153. */
  154. static noinline int join_transaction(struct btrfs_fs_info *fs_info,
  155. unsigned int type)
  156. {
  157. struct btrfs_transaction *cur_trans;
  158. spin_lock(&fs_info->trans_lock);
  159. loop:
  160. /* The file system has been taken offline. No new transactions. */
  161. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  162. spin_unlock(&fs_info->trans_lock);
  163. return -EROFS;
  164. }
  165. cur_trans = fs_info->running_transaction;
  166. if (cur_trans) {
  167. if (cur_trans->aborted) {
  168. spin_unlock(&fs_info->trans_lock);
  169. return cur_trans->aborted;
  170. }
  171. if (btrfs_blocked_trans_types[cur_trans->state] & type) {
  172. spin_unlock(&fs_info->trans_lock);
  173. return -EBUSY;
  174. }
  175. refcount_inc(&cur_trans->use_count);
  176. atomic_inc(&cur_trans->num_writers);
  177. extwriter_counter_inc(cur_trans, type);
  178. spin_unlock(&fs_info->trans_lock);
  179. return 0;
  180. }
  181. spin_unlock(&fs_info->trans_lock);
  182. /*
  183. * If we are ATTACH, we just want to catch the current transaction,
  184. * and commit it. If there is no transaction, just return ENOENT.
  185. */
  186. if (type == TRANS_ATTACH)
  187. return -ENOENT;
  188. /*
  189. * JOIN_NOLOCK only happens during the transaction commit, so
  190. * it is impossible that ->running_transaction is NULL
  191. */
  192. BUG_ON(type == TRANS_JOIN_NOLOCK);
  193. cur_trans = kmalloc(sizeof(*cur_trans), GFP_NOFS);
  194. if (!cur_trans)
  195. return -ENOMEM;
  196. spin_lock(&fs_info->trans_lock);
  197. if (fs_info->running_transaction) {
  198. /*
  199. * someone started a transaction after we unlocked. Make sure
  200. * to redo the checks above
  201. */
  202. kfree(cur_trans);
  203. goto loop;
  204. } else if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  205. spin_unlock(&fs_info->trans_lock);
  206. kfree(cur_trans);
  207. return -EROFS;
  208. }
  209. cur_trans->fs_info = fs_info;
  210. atomic_set(&cur_trans->num_writers, 1);
  211. extwriter_counter_init(cur_trans, type);
  212. init_waitqueue_head(&cur_trans->writer_wait);
  213. init_waitqueue_head(&cur_trans->commit_wait);
  214. init_waitqueue_head(&cur_trans->pending_wait);
  215. cur_trans->state = TRANS_STATE_RUNNING;
  216. /*
  217. * One for this trans handle, one so it will live on until we
  218. * commit the transaction.
  219. */
  220. refcount_set(&cur_trans->use_count, 2);
  221. atomic_set(&cur_trans->pending_ordered, 0);
  222. cur_trans->flags = 0;
  223. cur_trans->start_time = ktime_get_seconds();
  224. memset(&cur_trans->delayed_refs, 0, sizeof(cur_trans->delayed_refs));
  225. cur_trans->delayed_refs.href_root = RB_ROOT;
  226. cur_trans->delayed_refs.dirty_extent_root = RB_ROOT;
  227. atomic_set(&cur_trans->delayed_refs.num_entries, 0);
  228. /*
  229. * although the tree mod log is per file system and not per transaction,
  230. * the log must never go across transaction boundaries.
  231. */
  232. smp_mb();
  233. if (!list_empty(&fs_info->tree_mod_seq_list))
  234. WARN(1, KERN_ERR "BTRFS: tree_mod_seq_list not empty when creating a fresh transaction\n");
  235. if (!RB_EMPTY_ROOT(&fs_info->tree_mod_log))
  236. WARN(1, KERN_ERR "BTRFS: tree_mod_log rb tree not empty when creating a fresh transaction\n");
  237. atomic64_set(&fs_info->tree_mod_seq, 0);
  238. spin_lock_init(&cur_trans->delayed_refs.lock);
  239. INIT_LIST_HEAD(&cur_trans->pending_snapshots);
  240. INIT_LIST_HEAD(&cur_trans->pending_chunks);
  241. INIT_LIST_HEAD(&cur_trans->switch_commits);
  242. INIT_LIST_HEAD(&cur_trans->dirty_bgs);
  243. INIT_LIST_HEAD(&cur_trans->io_bgs);
  244. INIT_LIST_HEAD(&cur_trans->dropped_roots);
  245. mutex_init(&cur_trans->cache_write_mutex);
  246. cur_trans->num_dirty_bgs = 0;
  247. spin_lock_init(&cur_trans->dirty_bgs_lock);
  248. INIT_LIST_HEAD(&cur_trans->deleted_bgs);
  249. spin_lock_init(&cur_trans->dropped_roots_lock);
  250. list_add_tail(&cur_trans->list, &fs_info->trans_list);
  251. extent_io_tree_init(&cur_trans->dirty_pages,
  252. fs_info->btree_inode);
  253. fs_info->generation++;
  254. cur_trans->transid = fs_info->generation;
  255. fs_info->running_transaction = cur_trans;
  256. cur_trans->aborted = 0;
  257. spin_unlock(&fs_info->trans_lock);
  258. return 0;
  259. }
  260. /*
  261. * this does all the record keeping required to make sure that a reference
  262. * counted root is properly recorded in a given transaction. This is required
  263. * to make sure the old root from before we joined the transaction is deleted
  264. * when the transaction commits
  265. */
  266. static int record_root_in_trans(struct btrfs_trans_handle *trans,
  267. struct btrfs_root *root,
  268. int force)
  269. {
  270. struct btrfs_fs_info *fs_info = root->fs_info;
  271. if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  272. root->last_trans < trans->transid) || force) {
  273. WARN_ON(root == fs_info->extent_root);
  274. WARN_ON(!force && root->commit_root != root->node);
  275. /*
  276. * see below for IN_TRANS_SETUP usage rules
  277. * we have the reloc mutex held now, so there
  278. * is only one writer in this function
  279. */
  280. set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
  281. /* make sure readers find IN_TRANS_SETUP before
  282. * they find our root->last_trans update
  283. */
  284. smp_wmb();
  285. spin_lock(&fs_info->fs_roots_radix_lock);
  286. if (root->last_trans == trans->transid && !force) {
  287. spin_unlock(&fs_info->fs_roots_radix_lock);
  288. return 0;
  289. }
  290. radix_tree_tag_set(&fs_info->fs_roots_radix,
  291. (unsigned long)root->root_key.objectid,
  292. BTRFS_ROOT_TRANS_TAG);
  293. spin_unlock(&fs_info->fs_roots_radix_lock);
  294. root->last_trans = trans->transid;
  295. /* this is pretty tricky. We don't want to
  296. * take the relocation lock in btrfs_record_root_in_trans
  297. * unless we're really doing the first setup for this root in
  298. * this transaction.
  299. *
  300. * Normally we'd use root->last_trans as a flag to decide
  301. * if we want to take the expensive mutex.
  302. *
  303. * But, we have to set root->last_trans before we
  304. * init the relocation root, otherwise, we trip over warnings
  305. * in ctree.c. The solution used here is to flag ourselves
  306. * with root IN_TRANS_SETUP. When this is 1, we're still
  307. * fixing up the reloc trees and everyone must wait.
  308. *
  309. * When this is zero, they can trust root->last_trans and fly
  310. * through btrfs_record_root_in_trans without having to take the
  311. * lock. smp_wmb() makes sure that all the writes above are
  312. * done before we pop in the zero below
  313. */
  314. btrfs_init_reloc_root(trans, root);
  315. smp_mb__before_atomic();
  316. clear_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state);
  317. }
  318. return 0;
  319. }
  320. void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
  321. struct btrfs_root *root)
  322. {
  323. struct btrfs_fs_info *fs_info = root->fs_info;
  324. struct btrfs_transaction *cur_trans = trans->transaction;
  325. /* Add ourselves to the transaction dropped list */
  326. spin_lock(&cur_trans->dropped_roots_lock);
  327. list_add_tail(&root->root_list, &cur_trans->dropped_roots);
  328. spin_unlock(&cur_trans->dropped_roots_lock);
  329. /* Make sure we don't try to update the root at commit time */
  330. spin_lock(&fs_info->fs_roots_radix_lock);
  331. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  332. (unsigned long)root->root_key.objectid,
  333. BTRFS_ROOT_TRANS_TAG);
  334. spin_unlock(&fs_info->fs_roots_radix_lock);
  335. }
  336. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  337. struct btrfs_root *root)
  338. {
  339. struct btrfs_fs_info *fs_info = root->fs_info;
  340. if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  341. return 0;
  342. /*
  343. * see record_root_in_trans for comments about IN_TRANS_SETUP usage
  344. * and barriers
  345. */
  346. smp_rmb();
  347. if (root->last_trans == trans->transid &&
  348. !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
  349. return 0;
  350. mutex_lock(&fs_info->reloc_mutex);
  351. record_root_in_trans(trans, root, 0);
  352. mutex_unlock(&fs_info->reloc_mutex);
  353. return 0;
  354. }
  355. static inline int is_transaction_blocked(struct btrfs_transaction *trans)
  356. {
  357. return (trans->state >= TRANS_STATE_BLOCKED &&
  358. trans->state < TRANS_STATE_UNBLOCKED &&
  359. !trans->aborted);
  360. }
  361. /* wait for commit against the current transaction to become unblocked
  362. * when this is done, it is safe to start a new transaction, but the current
  363. * transaction might not be fully on disk.
  364. */
  365. static void wait_current_trans(struct btrfs_fs_info *fs_info)
  366. {
  367. struct btrfs_transaction *cur_trans;
  368. spin_lock(&fs_info->trans_lock);
  369. cur_trans = fs_info->running_transaction;
  370. if (cur_trans && is_transaction_blocked(cur_trans)) {
  371. refcount_inc(&cur_trans->use_count);
  372. spin_unlock(&fs_info->trans_lock);
  373. wait_event(fs_info->transaction_wait,
  374. cur_trans->state >= TRANS_STATE_UNBLOCKED ||
  375. cur_trans->aborted);
  376. btrfs_put_transaction(cur_trans);
  377. } else {
  378. spin_unlock(&fs_info->trans_lock);
  379. }
  380. }
  381. static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type)
  382. {
  383. if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
  384. return 0;
  385. if (type == TRANS_START)
  386. return 1;
  387. return 0;
  388. }
  389. static inline bool need_reserve_reloc_root(struct btrfs_root *root)
  390. {
  391. struct btrfs_fs_info *fs_info = root->fs_info;
  392. if (!fs_info->reloc_ctl ||
  393. !test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
  394. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
  395. root->reloc_root)
  396. return false;
  397. return true;
  398. }
  399. static struct btrfs_trans_handle *
  400. start_transaction(struct btrfs_root *root, unsigned int num_items,
  401. unsigned int type, enum btrfs_reserve_flush_enum flush,
  402. bool enforce_qgroups)
  403. {
  404. struct btrfs_fs_info *fs_info = root->fs_info;
  405. struct btrfs_trans_handle *h;
  406. struct btrfs_transaction *cur_trans;
  407. u64 num_bytes = 0;
  408. u64 qgroup_reserved = 0;
  409. bool reloc_reserved = false;
  410. int ret;
  411. /* Send isn't supposed to start transactions. */
  412. ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB);
  413. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  414. return ERR_PTR(-EROFS);
  415. if (current->journal_info) {
  416. WARN_ON(type & TRANS_EXTWRITERS);
  417. h = current->journal_info;
  418. refcount_inc(&h->use_count);
  419. WARN_ON(refcount_read(&h->use_count) > 2);
  420. h->orig_rsv = h->block_rsv;
  421. h->block_rsv = NULL;
  422. goto got_it;
  423. }
  424. /*
  425. * Do the reservation before we join the transaction so we can do all
  426. * the appropriate flushing if need be.
  427. */
  428. if (num_items && root != fs_info->chunk_root) {
  429. qgroup_reserved = num_items * fs_info->nodesize;
  430. ret = btrfs_qgroup_reserve_meta_pertrans(root, qgroup_reserved,
  431. enforce_qgroups);
  432. if (ret)
  433. return ERR_PTR(ret);
  434. num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items);
  435. /*
  436. * Do the reservation for the relocation root creation
  437. */
  438. if (need_reserve_reloc_root(root)) {
  439. num_bytes += fs_info->nodesize;
  440. reloc_reserved = true;
  441. }
  442. ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
  443. num_bytes, flush);
  444. if (ret)
  445. goto reserve_fail;
  446. }
  447. again:
  448. h = kmem_cache_zalloc(btrfs_trans_handle_cachep, GFP_NOFS);
  449. if (!h) {
  450. ret = -ENOMEM;
  451. goto alloc_fail;
  452. }
  453. /*
  454. * If we are JOIN_NOLOCK we're already committing a transaction and
  455. * waiting on this guy, so we don't need to do the sb_start_intwrite
  456. * because we're already holding a ref. We need this because we could
  457. * have raced in and did an fsync() on a file which can kick a commit
  458. * and then we deadlock with somebody doing a freeze.
  459. *
  460. * If we are ATTACH, it means we just want to catch the current
  461. * transaction and commit it, so we needn't do sb_start_intwrite().
  462. */
  463. if (type & __TRANS_FREEZABLE)
  464. sb_start_intwrite(fs_info->sb);
  465. if (may_wait_transaction(fs_info, type))
  466. wait_current_trans(fs_info);
  467. do {
  468. ret = join_transaction(fs_info, type);
  469. if (ret == -EBUSY) {
  470. wait_current_trans(fs_info);
  471. if (unlikely(type == TRANS_ATTACH ||
  472. type == TRANS_JOIN_NOSTART))
  473. ret = -ENOENT;
  474. }
  475. } while (ret == -EBUSY);
  476. if (ret < 0)
  477. goto join_fail;
  478. cur_trans = fs_info->running_transaction;
  479. h->transid = cur_trans->transid;
  480. h->transaction = cur_trans;
  481. h->root = root;
  482. refcount_set(&h->use_count, 1);
  483. h->fs_info = root->fs_info;
  484. h->type = type;
  485. h->can_flush_pending_bgs = true;
  486. INIT_LIST_HEAD(&h->new_bgs);
  487. smp_mb();
  488. if (cur_trans->state >= TRANS_STATE_BLOCKED &&
  489. may_wait_transaction(fs_info, type)) {
  490. current->journal_info = h;
  491. btrfs_commit_transaction(h);
  492. goto again;
  493. }
  494. if (num_bytes) {
  495. trace_btrfs_space_reservation(fs_info, "transaction",
  496. h->transid, num_bytes, 1);
  497. h->block_rsv = &fs_info->trans_block_rsv;
  498. h->bytes_reserved = num_bytes;
  499. h->reloc_reserved = reloc_reserved;
  500. }
  501. got_it:
  502. if (!current->journal_info)
  503. current->journal_info = h;
  504. /*
  505. * btrfs_record_root_in_trans() needs to alloc new extents, and may
  506. * call btrfs_join_transaction() while we're also starting a
  507. * transaction.
  508. *
  509. * Thus it need to be called after current->journal_info initialized,
  510. * or we can deadlock.
  511. */
  512. btrfs_record_root_in_trans(h, root);
  513. return h;
  514. join_fail:
  515. if (type & __TRANS_FREEZABLE)
  516. sb_end_intwrite(fs_info->sb);
  517. kmem_cache_free(btrfs_trans_handle_cachep, h);
  518. alloc_fail:
  519. if (num_bytes)
  520. btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv,
  521. num_bytes);
  522. reserve_fail:
  523. btrfs_qgroup_free_meta_pertrans(root, qgroup_reserved);
  524. return ERR_PTR(ret);
  525. }
  526. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  527. unsigned int num_items)
  528. {
  529. return start_transaction(root, num_items, TRANS_START,
  530. BTRFS_RESERVE_FLUSH_ALL, true);
  531. }
  532. struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
  533. struct btrfs_root *root,
  534. unsigned int num_items,
  535. int min_factor)
  536. {
  537. struct btrfs_fs_info *fs_info = root->fs_info;
  538. struct btrfs_trans_handle *trans;
  539. u64 num_bytes;
  540. int ret;
  541. /*
  542. * We have two callers: unlink and block group removal. The
  543. * former should succeed even if we will temporarily exceed
  544. * quota and the latter operates on the extent root so
  545. * qgroup enforcement is ignored anyway.
  546. */
  547. trans = start_transaction(root, num_items, TRANS_START,
  548. BTRFS_RESERVE_FLUSH_ALL, false);
  549. if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
  550. return trans;
  551. trans = btrfs_start_transaction(root, 0);
  552. if (IS_ERR(trans))
  553. return trans;
  554. num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items);
  555. ret = btrfs_cond_migrate_bytes(fs_info, &fs_info->trans_block_rsv,
  556. num_bytes, min_factor);
  557. if (ret) {
  558. btrfs_end_transaction(trans);
  559. return ERR_PTR(ret);
  560. }
  561. trans->block_rsv = &fs_info->trans_block_rsv;
  562. trans->bytes_reserved = num_bytes;
  563. trace_btrfs_space_reservation(fs_info, "transaction",
  564. trans->transid, num_bytes, 1);
  565. return trans;
  566. }
  567. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
  568. {
  569. return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH,
  570. true);
  571. }
  572. struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
  573. {
  574. return start_transaction(root, 0, TRANS_JOIN_NOLOCK,
  575. BTRFS_RESERVE_NO_FLUSH, true);
  576. }
  577. /*
  578. * Similar to regular join but it never starts a transaction when none is
  579. * running or after waiting for the current one to finish.
  580. */
  581. struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root)
  582. {
  583. return start_transaction(root, 0, TRANS_JOIN_NOSTART,
  584. BTRFS_RESERVE_NO_FLUSH, true);
  585. }
  586. /*
  587. * btrfs_attach_transaction() - catch the running transaction
  588. *
  589. * It is used when we want to commit the current the transaction, but
  590. * don't want to start a new one.
  591. *
  592. * Note: If this function return -ENOENT, it just means there is no
  593. * running transaction. But it is possible that the inactive transaction
  594. * is still in the memory, not fully on disk. If you hope there is no
  595. * inactive transaction in the fs when -ENOENT is returned, you should
  596. * invoke
  597. * btrfs_attach_transaction_barrier()
  598. */
  599. struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
  600. {
  601. return start_transaction(root, 0, TRANS_ATTACH,
  602. BTRFS_RESERVE_NO_FLUSH, true);
  603. }
  604. /*
  605. * btrfs_attach_transaction_barrier() - catch the running transaction
  606. *
  607. * It is similar to the above function, the differentia is this one
  608. * will wait for all the inactive transactions until they fully
  609. * complete.
  610. */
  611. struct btrfs_trans_handle *
  612. btrfs_attach_transaction_barrier(struct btrfs_root *root)
  613. {
  614. struct btrfs_trans_handle *trans;
  615. trans = start_transaction(root, 0, TRANS_ATTACH,
  616. BTRFS_RESERVE_NO_FLUSH, true);
  617. if (trans == ERR_PTR(-ENOENT))
  618. btrfs_wait_for_commit(root->fs_info, 0);
  619. return trans;
  620. }
  621. /* wait for a transaction commit to be fully complete */
  622. static noinline void wait_for_commit(struct btrfs_transaction *commit)
  623. {
  624. wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
  625. }
  626. int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
  627. {
  628. struct btrfs_transaction *cur_trans = NULL, *t;
  629. int ret = 0;
  630. if (transid) {
  631. if (transid <= fs_info->last_trans_committed)
  632. goto out;
  633. /* find specified transaction */
  634. spin_lock(&fs_info->trans_lock);
  635. list_for_each_entry(t, &fs_info->trans_list, list) {
  636. if (t->transid == transid) {
  637. cur_trans = t;
  638. refcount_inc(&cur_trans->use_count);
  639. ret = 0;
  640. break;
  641. }
  642. if (t->transid > transid) {
  643. ret = 0;
  644. break;
  645. }
  646. }
  647. spin_unlock(&fs_info->trans_lock);
  648. /*
  649. * The specified transaction doesn't exist, or we
  650. * raced with btrfs_commit_transaction
  651. */
  652. if (!cur_trans) {
  653. if (transid > fs_info->last_trans_committed)
  654. ret = -EINVAL;
  655. goto out;
  656. }
  657. } else {
  658. /* find newest transaction that is committing | committed */
  659. spin_lock(&fs_info->trans_lock);
  660. list_for_each_entry_reverse(t, &fs_info->trans_list,
  661. list) {
  662. if (t->state >= TRANS_STATE_COMMIT_START) {
  663. if (t->state == TRANS_STATE_COMPLETED)
  664. break;
  665. cur_trans = t;
  666. refcount_inc(&cur_trans->use_count);
  667. break;
  668. }
  669. }
  670. spin_unlock(&fs_info->trans_lock);
  671. if (!cur_trans)
  672. goto out; /* nothing committing|committed */
  673. }
  674. wait_for_commit(cur_trans);
  675. btrfs_put_transaction(cur_trans);
  676. out:
  677. return ret;
  678. }
  679. void btrfs_throttle(struct btrfs_fs_info *fs_info)
  680. {
  681. wait_current_trans(fs_info);
  682. }
  683. static int should_end_transaction(struct btrfs_trans_handle *trans)
  684. {
  685. struct btrfs_fs_info *fs_info = trans->fs_info;
  686. if (btrfs_check_space_for_delayed_refs(trans, fs_info))
  687. return 1;
  688. return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5);
  689. }
  690. int btrfs_should_end_transaction(struct btrfs_trans_handle *trans)
  691. {
  692. struct btrfs_transaction *cur_trans = trans->transaction;
  693. int updates;
  694. int err;
  695. smp_mb();
  696. if (cur_trans->state >= TRANS_STATE_BLOCKED ||
  697. cur_trans->delayed_refs.flushing)
  698. return 1;
  699. updates = trans->delayed_ref_updates;
  700. trans->delayed_ref_updates = 0;
  701. if (updates) {
  702. err = btrfs_run_delayed_refs(trans, updates * 2);
  703. if (err) /* Error code will also eval true */
  704. return err;
  705. }
  706. return should_end_transaction(trans);
  707. }
  708. static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans)
  709. {
  710. struct btrfs_fs_info *fs_info = trans->fs_info;
  711. if (!trans->block_rsv) {
  712. ASSERT(!trans->bytes_reserved);
  713. return;
  714. }
  715. if (!trans->bytes_reserved)
  716. return;
  717. ASSERT(trans->block_rsv == &fs_info->trans_block_rsv);
  718. trace_btrfs_space_reservation(fs_info, "transaction",
  719. trans->transid, trans->bytes_reserved, 0);
  720. btrfs_block_rsv_release(fs_info, trans->block_rsv,
  721. trans->bytes_reserved);
  722. trans->bytes_reserved = 0;
  723. }
  724. static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
  725. int throttle)
  726. {
  727. struct btrfs_fs_info *info = trans->fs_info;
  728. struct btrfs_transaction *cur_trans = trans->transaction;
  729. u64 transid = trans->transid;
  730. unsigned long cur = trans->delayed_ref_updates;
  731. int lock = (trans->type != TRANS_JOIN_NOLOCK);
  732. int err = 0;
  733. int must_run_delayed_refs = 0;
  734. if (refcount_read(&trans->use_count) > 1) {
  735. refcount_dec(&trans->use_count);
  736. trans->block_rsv = trans->orig_rsv;
  737. return 0;
  738. }
  739. btrfs_trans_release_metadata(trans);
  740. trans->block_rsv = NULL;
  741. if (!list_empty(&trans->new_bgs))
  742. btrfs_create_pending_block_groups(trans);
  743. trans->delayed_ref_updates = 0;
  744. if (!trans->sync) {
  745. must_run_delayed_refs =
  746. btrfs_should_throttle_delayed_refs(trans, info);
  747. cur = max_t(unsigned long, cur, 32);
  748. /*
  749. * don't make the caller wait if they are from a NOLOCK
  750. * or ATTACH transaction, it will deadlock with commit
  751. */
  752. if (must_run_delayed_refs == 1 &&
  753. (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH)))
  754. must_run_delayed_refs = 2;
  755. }
  756. btrfs_trans_release_metadata(trans);
  757. trans->block_rsv = NULL;
  758. if (!list_empty(&trans->new_bgs))
  759. btrfs_create_pending_block_groups(trans);
  760. btrfs_trans_release_chunk_metadata(trans);
  761. if (lock && should_end_transaction(trans) &&
  762. READ_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
  763. spin_lock(&info->trans_lock);
  764. if (cur_trans->state == TRANS_STATE_RUNNING)
  765. cur_trans->state = TRANS_STATE_BLOCKED;
  766. spin_unlock(&info->trans_lock);
  767. }
  768. if (lock && READ_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
  769. if (throttle)
  770. return btrfs_commit_transaction(trans);
  771. else
  772. wake_up_process(info->transaction_kthread);
  773. }
  774. if (trans->type & __TRANS_FREEZABLE)
  775. sb_end_intwrite(info->sb);
  776. WARN_ON(cur_trans != info->running_transaction);
  777. WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
  778. atomic_dec(&cur_trans->num_writers);
  779. extwriter_counter_dec(cur_trans, trans->type);
  780. cond_wake_up(&cur_trans->writer_wait);
  781. btrfs_put_transaction(cur_trans);
  782. if (current->journal_info == trans)
  783. current->journal_info = NULL;
  784. if (throttle)
  785. btrfs_run_delayed_iputs(info);
  786. if (trans->aborted ||
  787. test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) {
  788. wake_up_process(info->transaction_kthread);
  789. err = -EIO;
  790. }
  791. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  792. if (must_run_delayed_refs) {
  793. btrfs_async_run_delayed_refs(info, cur, transid,
  794. must_run_delayed_refs == 1);
  795. }
  796. return err;
  797. }
  798. int btrfs_end_transaction(struct btrfs_trans_handle *trans)
  799. {
  800. return __btrfs_end_transaction(trans, 0);
  801. }
  802. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans)
  803. {
  804. return __btrfs_end_transaction(trans, 1);
  805. }
  806. /*
  807. * when btree blocks are allocated, they have some corresponding bits set for
  808. * them in one of two extent_io trees. This is used to make sure all of
  809. * those extents are sent to disk but does not wait on them
  810. */
  811. int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
  812. struct extent_io_tree *dirty_pages, int mark)
  813. {
  814. int err = 0;
  815. int werr = 0;
  816. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  817. struct extent_state *cached_state = NULL;
  818. u64 start = 0;
  819. u64 end;
  820. atomic_inc(&BTRFS_I(fs_info->btree_inode)->sync_writers);
  821. while (!find_first_extent_bit(dirty_pages, start, &start, &end,
  822. mark, &cached_state)) {
  823. bool wait_writeback = false;
  824. err = convert_extent_bit(dirty_pages, start, end,
  825. EXTENT_NEED_WAIT,
  826. mark, &cached_state);
  827. /*
  828. * convert_extent_bit can return -ENOMEM, which is most of the
  829. * time a temporary error. So when it happens, ignore the error
  830. * and wait for writeback of this range to finish - because we
  831. * failed to set the bit EXTENT_NEED_WAIT for the range, a call
  832. * to __btrfs_wait_marked_extents() would not know that
  833. * writeback for this range started and therefore wouldn't
  834. * wait for it to finish - we don't want to commit a
  835. * superblock that points to btree nodes/leafs for which
  836. * writeback hasn't finished yet (and without errors).
  837. * We cleanup any entries left in the io tree when committing
  838. * the transaction (through clear_btree_io_tree()).
  839. */
  840. if (err == -ENOMEM) {
  841. err = 0;
  842. wait_writeback = true;
  843. }
  844. if (!err)
  845. err = filemap_fdatawrite_range(mapping, start, end);
  846. if (err)
  847. werr = err;
  848. else if (wait_writeback)
  849. werr = filemap_fdatawait_range(mapping, start, end);
  850. free_extent_state(cached_state);
  851. cached_state = NULL;
  852. cond_resched();
  853. start = end + 1;
  854. }
  855. atomic_dec(&BTRFS_I(fs_info->btree_inode)->sync_writers);
  856. return werr;
  857. }
  858. /*
  859. * when btree blocks are allocated, they have some corresponding bits set for
  860. * them in one of two extent_io trees. This is used to make sure all of
  861. * those extents are on disk for transaction or log commit. We wait
  862. * on all the pages and clear them from the dirty pages state tree
  863. */
  864. static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info,
  865. struct extent_io_tree *dirty_pages)
  866. {
  867. int err = 0;
  868. int werr = 0;
  869. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  870. struct extent_state *cached_state = NULL;
  871. u64 start = 0;
  872. u64 end;
  873. while (!find_first_extent_bit(dirty_pages, start, &start, &end,
  874. EXTENT_NEED_WAIT, &cached_state)) {
  875. /*
  876. * Ignore -ENOMEM errors returned by clear_extent_bit().
  877. * When committing the transaction, we'll remove any entries
  878. * left in the io tree. For a log commit, we don't remove them
  879. * after committing the log because the tree can be accessed
  880. * concurrently - we do it only at transaction commit time when
  881. * it's safe to do it (through clear_btree_io_tree()).
  882. */
  883. err = clear_extent_bit(dirty_pages, start, end,
  884. EXTENT_NEED_WAIT, 0, 0, &cached_state);
  885. if (err == -ENOMEM)
  886. err = 0;
  887. if (!err)
  888. err = filemap_fdatawait_range(mapping, start, end);
  889. if (err)
  890. werr = err;
  891. free_extent_state(cached_state);
  892. cached_state = NULL;
  893. cond_resched();
  894. start = end + 1;
  895. }
  896. if (err)
  897. werr = err;
  898. return werr;
  899. }
  900. int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
  901. struct extent_io_tree *dirty_pages)
  902. {
  903. bool errors = false;
  904. int err;
  905. err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
  906. if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags))
  907. errors = true;
  908. if (errors && !err)
  909. err = -EIO;
  910. return err;
  911. }
  912. int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark)
  913. {
  914. struct btrfs_fs_info *fs_info = log_root->fs_info;
  915. struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages;
  916. bool errors = false;
  917. int err;
  918. ASSERT(log_root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
  919. err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
  920. if ((mark & EXTENT_DIRTY) &&
  921. test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags))
  922. errors = true;
  923. if ((mark & EXTENT_NEW) &&
  924. test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags))
  925. errors = true;
  926. if (errors && !err)
  927. err = -EIO;
  928. return err;
  929. }
  930. /*
  931. * When btree blocks are allocated the corresponding extents are marked dirty.
  932. * This function ensures such extents are persisted on disk for transaction or
  933. * log commit.
  934. *
  935. * @trans: transaction whose dirty pages we'd like to write
  936. */
  937. static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans)
  938. {
  939. int ret;
  940. int ret2;
  941. struct extent_io_tree *dirty_pages = &trans->transaction->dirty_pages;
  942. struct btrfs_fs_info *fs_info = trans->fs_info;
  943. struct blk_plug plug;
  944. blk_start_plug(&plug);
  945. ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY);
  946. blk_finish_plug(&plug);
  947. ret2 = btrfs_wait_extents(fs_info, dirty_pages);
  948. clear_btree_io_tree(&trans->transaction->dirty_pages);
  949. if (ret)
  950. return ret;
  951. else if (ret2)
  952. return ret2;
  953. else
  954. return 0;
  955. }
  956. /*
  957. * this is used to update the root pointer in the tree of tree roots.
  958. *
  959. * But, in the case of the extent allocation tree, updating the root
  960. * pointer may allocate blocks which may change the root of the extent
  961. * allocation tree.
  962. *
  963. * So, this loops and repeats and makes sure the cowonly root didn't
  964. * change while the root pointer was being updated in the metadata.
  965. */
  966. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  967. struct btrfs_root *root)
  968. {
  969. int ret;
  970. u64 old_root_bytenr;
  971. u64 old_root_used;
  972. struct btrfs_fs_info *fs_info = root->fs_info;
  973. struct btrfs_root *tree_root = fs_info->tree_root;
  974. old_root_used = btrfs_root_used(&root->root_item);
  975. while (1) {
  976. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  977. if (old_root_bytenr == root->node->start &&
  978. old_root_used == btrfs_root_used(&root->root_item))
  979. break;
  980. btrfs_set_root_node(&root->root_item, root->node);
  981. ret = btrfs_update_root(trans, tree_root,
  982. &root->root_key,
  983. &root->root_item);
  984. if (ret)
  985. return ret;
  986. old_root_used = btrfs_root_used(&root->root_item);
  987. }
  988. return 0;
  989. }
  990. /*
  991. * update all the cowonly tree roots on disk
  992. *
  993. * The error handling in this function may not be obvious. Any of the
  994. * failures will cause the file system to go offline. We still need
  995. * to clean up the delayed refs.
  996. */
  997. static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
  998. {
  999. struct btrfs_fs_info *fs_info = trans->fs_info;
  1000. struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
  1001. struct list_head *io_bgs = &trans->transaction->io_bgs;
  1002. struct list_head *next;
  1003. struct extent_buffer *eb;
  1004. int ret;
  1005. eb = btrfs_lock_root_node(fs_info->tree_root);
  1006. ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
  1007. 0, &eb);
  1008. btrfs_tree_unlock(eb);
  1009. free_extent_buffer(eb);
  1010. if (ret)
  1011. return ret;
  1012. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1013. if (ret)
  1014. return ret;
  1015. ret = btrfs_run_dev_stats(trans, fs_info);
  1016. if (ret)
  1017. return ret;
  1018. ret = btrfs_run_dev_replace(trans, fs_info);
  1019. if (ret)
  1020. return ret;
  1021. ret = btrfs_run_qgroups(trans);
  1022. if (ret)
  1023. return ret;
  1024. ret = btrfs_setup_space_cache(trans, fs_info);
  1025. if (ret)
  1026. return ret;
  1027. /* run_qgroups might have added some more refs */
  1028. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1029. if (ret)
  1030. return ret;
  1031. again:
  1032. while (!list_empty(&fs_info->dirty_cowonly_roots)) {
  1033. struct btrfs_root *root;
  1034. next = fs_info->dirty_cowonly_roots.next;
  1035. list_del_init(next);
  1036. root = list_entry(next, struct btrfs_root, dirty_list);
  1037. clear_bit(BTRFS_ROOT_DIRTY, &root->state);
  1038. if (root != fs_info->extent_root)
  1039. list_add_tail(&root->dirty_list,
  1040. &trans->transaction->switch_commits);
  1041. ret = update_cowonly_root(trans, root);
  1042. if (ret)
  1043. return ret;
  1044. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1045. if (ret)
  1046. return ret;
  1047. }
  1048. while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
  1049. ret = btrfs_write_dirty_block_groups(trans, fs_info);
  1050. if (ret)
  1051. return ret;
  1052. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1053. if (ret)
  1054. return ret;
  1055. }
  1056. if (!list_empty(&fs_info->dirty_cowonly_roots))
  1057. goto again;
  1058. list_add_tail(&fs_info->extent_root->dirty_list,
  1059. &trans->transaction->switch_commits);
  1060. btrfs_after_dev_replace_commit(fs_info);
  1061. return 0;
  1062. }
  1063. /*
  1064. * dead roots are old snapshots that need to be deleted. This allocates
  1065. * a dirty root struct and adds it into the list of dead roots that need to
  1066. * be deleted
  1067. */
  1068. void btrfs_add_dead_root(struct btrfs_root *root)
  1069. {
  1070. struct btrfs_fs_info *fs_info = root->fs_info;
  1071. spin_lock(&fs_info->trans_lock);
  1072. if (list_empty(&root->root_list))
  1073. list_add_tail(&root->root_list, &fs_info->dead_roots);
  1074. spin_unlock(&fs_info->trans_lock);
  1075. }
  1076. /*
  1077. * update all the cowonly tree roots on disk
  1078. */
  1079. static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
  1080. {
  1081. struct btrfs_fs_info *fs_info = trans->fs_info;
  1082. struct btrfs_root *gang[8];
  1083. int i;
  1084. int ret;
  1085. spin_lock(&fs_info->fs_roots_radix_lock);
  1086. while (1) {
  1087. ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
  1088. (void **)gang, 0,
  1089. ARRAY_SIZE(gang),
  1090. BTRFS_ROOT_TRANS_TAG);
  1091. if (ret == 0)
  1092. break;
  1093. for (i = 0; i < ret; i++) {
  1094. struct btrfs_root *root = gang[i];
  1095. int ret2;
  1096. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  1097. (unsigned long)root->root_key.objectid,
  1098. BTRFS_ROOT_TRANS_TAG);
  1099. spin_unlock(&fs_info->fs_roots_radix_lock);
  1100. btrfs_free_log(trans, root);
  1101. btrfs_update_reloc_root(trans, root);
  1102. btrfs_save_ino_cache(root, trans);
  1103. /* see comments in should_cow_block() */
  1104. clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
  1105. smp_mb__after_atomic();
  1106. if (root->commit_root != root->node) {
  1107. list_add_tail(&root->dirty_list,
  1108. &trans->transaction->switch_commits);
  1109. btrfs_set_root_node(&root->root_item,
  1110. root->node);
  1111. }
  1112. ret2 = btrfs_update_root(trans, fs_info->tree_root,
  1113. &root->root_key,
  1114. &root->root_item);
  1115. if (ret2)
  1116. return ret2;
  1117. spin_lock(&fs_info->fs_roots_radix_lock);
  1118. btrfs_qgroup_free_meta_all_pertrans(root);
  1119. }
  1120. }
  1121. spin_unlock(&fs_info->fs_roots_radix_lock);
  1122. return 0;
  1123. }
  1124. /*
  1125. * defrag a given btree.
  1126. * Every leaf in the btree is read and defragged.
  1127. */
  1128. int btrfs_defrag_root(struct btrfs_root *root)
  1129. {
  1130. struct btrfs_fs_info *info = root->fs_info;
  1131. struct btrfs_trans_handle *trans;
  1132. int ret;
  1133. if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
  1134. return 0;
  1135. while (1) {
  1136. trans = btrfs_start_transaction(root, 0);
  1137. if (IS_ERR(trans))
  1138. return PTR_ERR(trans);
  1139. ret = btrfs_defrag_leaves(trans, root);
  1140. btrfs_end_transaction(trans);
  1141. btrfs_btree_balance_dirty(info);
  1142. cond_resched();
  1143. if (btrfs_fs_closing(info) || ret != -EAGAIN)
  1144. break;
  1145. if (btrfs_defrag_cancelled(info)) {
  1146. btrfs_debug(info, "defrag_root cancelled");
  1147. ret = -EAGAIN;
  1148. break;
  1149. }
  1150. }
  1151. clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
  1152. return ret;
  1153. }
  1154. /*
  1155. * Do all special snapshot related qgroup dirty hack.
  1156. *
  1157. * Will do all needed qgroup inherit and dirty hack like switch commit
  1158. * roots inside one transaction and write all btree into disk, to make
  1159. * qgroup works.
  1160. */
  1161. static int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
  1162. struct btrfs_root *src,
  1163. struct btrfs_root *parent,
  1164. struct btrfs_qgroup_inherit *inherit,
  1165. u64 dst_objectid)
  1166. {
  1167. struct btrfs_fs_info *fs_info = src->fs_info;
  1168. int ret;
  1169. /*
  1170. * Save some performance in the case that qgroups are not
  1171. * enabled. If this check races with the ioctl, rescan will
  1172. * kick in anyway.
  1173. */
  1174. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1175. return 0;
  1176. /*
  1177. * Ensure dirty @src will be commited. Or, after comming
  1178. * commit_fs_roots() and switch_commit_roots(), any dirty but not
  1179. * recorded root will never be updated again, causing an outdated root
  1180. * item.
  1181. */
  1182. record_root_in_trans(trans, src, 1);
  1183. /*
  1184. * We are going to commit transaction, see btrfs_commit_transaction()
  1185. * comment for reason locking tree_log_mutex
  1186. */
  1187. mutex_lock(&fs_info->tree_log_mutex);
  1188. ret = commit_fs_roots(trans);
  1189. if (ret)
  1190. goto out;
  1191. ret = btrfs_qgroup_account_extents(trans);
  1192. if (ret < 0)
  1193. goto out;
  1194. /* Now qgroup are all updated, we can inherit it to new qgroups */
  1195. ret = btrfs_qgroup_inherit(trans, src->root_key.objectid, dst_objectid,
  1196. inherit);
  1197. if (ret < 0)
  1198. goto out;
  1199. /*
  1200. * Now we do a simplified commit transaction, which will:
  1201. * 1) commit all subvolume and extent tree
  1202. * To ensure all subvolume and extent tree have a valid
  1203. * commit_root to accounting later insert_dir_item()
  1204. * 2) write all btree blocks onto disk
  1205. * This is to make sure later btree modification will be cowed
  1206. * Or commit_root can be populated and cause wrong qgroup numbers
  1207. * In this simplified commit, we don't really care about other trees
  1208. * like chunk and root tree, as they won't affect qgroup.
  1209. * And we don't write super to avoid half committed status.
  1210. */
  1211. ret = commit_cowonly_roots(trans);
  1212. if (ret)
  1213. goto out;
  1214. switch_commit_roots(trans->transaction);
  1215. ret = btrfs_write_and_wait_transaction(trans);
  1216. if (ret)
  1217. btrfs_handle_fs_error(fs_info, ret,
  1218. "Error while writing out transaction for qgroup");
  1219. out:
  1220. mutex_unlock(&fs_info->tree_log_mutex);
  1221. /*
  1222. * Force parent root to be updated, as we recorded it before so its
  1223. * last_trans == cur_transid.
  1224. * Or it won't be committed again onto disk after later
  1225. * insert_dir_item()
  1226. */
  1227. if (!ret)
  1228. record_root_in_trans(trans, parent, 1);
  1229. return ret;
  1230. }
  1231. /*
  1232. * new snapshots need to be created at a very specific time in the
  1233. * transaction commit. This does the actual creation.
  1234. *
  1235. * Note:
  1236. * If the error which may affect the commitment of the current transaction
  1237. * happens, we should return the error number. If the error which just affect
  1238. * the creation of the pending snapshots, just return 0.
  1239. */
  1240. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  1241. struct btrfs_pending_snapshot *pending)
  1242. {
  1243. struct btrfs_fs_info *fs_info = trans->fs_info;
  1244. struct btrfs_key key;
  1245. struct btrfs_root_item *new_root_item;
  1246. struct btrfs_root *tree_root = fs_info->tree_root;
  1247. struct btrfs_root *root = pending->root;
  1248. struct btrfs_root *parent_root;
  1249. struct btrfs_block_rsv *rsv;
  1250. struct inode *parent_inode;
  1251. struct btrfs_path *path;
  1252. struct btrfs_dir_item *dir_item;
  1253. struct dentry *dentry;
  1254. struct extent_buffer *tmp;
  1255. struct extent_buffer *old;
  1256. struct timespec64 cur_time;
  1257. int ret = 0;
  1258. u64 to_reserve = 0;
  1259. u64 index = 0;
  1260. u64 objectid;
  1261. u64 root_flags;
  1262. uuid_le new_uuid;
  1263. ASSERT(pending->path);
  1264. path = pending->path;
  1265. ASSERT(pending->root_item);
  1266. new_root_item = pending->root_item;
  1267. pending->error = btrfs_find_free_objectid(tree_root, &objectid);
  1268. if (pending->error)
  1269. goto no_free_objectid;
  1270. /*
  1271. * Make qgroup to skip current new snapshot's qgroupid, as it is
  1272. * accounted by later btrfs_qgroup_inherit().
  1273. */
  1274. btrfs_set_skip_qgroup(trans, objectid);
  1275. btrfs_reloc_pre_snapshot(pending, &to_reserve);
  1276. if (to_reserve > 0) {
  1277. pending->error = btrfs_block_rsv_add(root,
  1278. &pending->block_rsv,
  1279. to_reserve,
  1280. BTRFS_RESERVE_NO_FLUSH);
  1281. if (pending->error)
  1282. goto clear_skip_qgroup;
  1283. }
  1284. key.objectid = objectid;
  1285. key.offset = (u64)-1;
  1286. key.type = BTRFS_ROOT_ITEM_KEY;
  1287. rsv = trans->block_rsv;
  1288. trans->block_rsv = &pending->block_rsv;
  1289. trans->bytes_reserved = trans->block_rsv->reserved;
  1290. trace_btrfs_space_reservation(fs_info, "transaction",
  1291. trans->transid,
  1292. trans->bytes_reserved, 1);
  1293. dentry = pending->dentry;
  1294. parent_inode = pending->dir;
  1295. parent_root = BTRFS_I(parent_inode)->root;
  1296. record_root_in_trans(trans, parent_root, 0);
  1297. cur_time = current_time(parent_inode);
  1298. /*
  1299. * insert the directory item
  1300. */
  1301. ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index);
  1302. BUG_ON(ret); /* -ENOMEM */
  1303. /* check if there is a file/dir which has the same name. */
  1304. dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
  1305. btrfs_ino(BTRFS_I(parent_inode)),
  1306. dentry->d_name.name,
  1307. dentry->d_name.len, 0);
  1308. if (dir_item != NULL && !IS_ERR(dir_item)) {
  1309. pending->error = -EEXIST;
  1310. goto dir_item_existed;
  1311. } else if (IS_ERR(dir_item)) {
  1312. ret = PTR_ERR(dir_item);
  1313. btrfs_abort_transaction(trans, ret);
  1314. goto fail;
  1315. }
  1316. btrfs_release_path(path);
  1317. /*
  1318. * pull in the delayed directory update
  1319. * and the delayed inode item
  1320. * otherwise we corrupt the FS during
  1321. * snapshot
  1322. */
  1323. ret = btrfs_run_delayed_items(trans);
  1324. if (ret) { /* Transaction aborted */
  1325. btrfs_abort_transaction(trans, ret);
  1326. goto fail;
  1327. }
  1328. record_root_in_trans(trans, root, 0);
  1329. btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  1330. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  1331. btrfs_check_and_init_root_item(new_root_item);
  1332. root_flags = btrfs_root_flags(new_root_item);
  1333. if (pending->readonly)
  1334. root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
  1335. else
  1336. root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
  1337. btrfs_set_root_flags(new_root_item, root_flags);
  1338. btrfs_set_root_generation_v2(new_root_item,
  1339. trans->transid);
  1340. uuid_le_gen(&new_uuid);
  1341. memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
  1342. memcpy(new_root_item->parent_uuid, root->root_item.uuid,
  1343. BTRFS_UUID_SIZE);
  1344. if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
  1345. memset(new_root_item->received_uuid, 0,
  1346. sizeof(new_root_item->received_uuid));
  1347. memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
  1348. memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
  1349. btrfs_set_root_stransid(new_root_item, 0);
  1350. btrfs_set_root_rtransid(new_root_item, 0);
  1351. }
  1352. btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
  1353. btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
  1354. btrfs_set_root_otransid(new_root_item, trans->transid);
  1355. old = btrfs_lock_root_node(root);
  1356. ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
  1357. if (ret) {
  1358. btrfs_tree_unlock(old);
  1359. free_extent_buffer(old);
  1360. btrfs_abort_transaction(trans, ret);
  1361. goto fail;
  1362. }
  1363. btrfs_set_lock_blocking(old);
  1364. ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
  1365. /* clean up in any case */
  1366. btrfs_tree_unlock(old);
  1367. free_extent_buffer(old);
  1368. if (ret) {
  1369. btrfs_abort_transaction(trans, ret);
  1370. goto fail;
  1371. }
  1372. /* see comments in should_cow_block() */
  1373. set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
  1374. smp_wmb();
  1375. btrfs_set_root_node(new_root_item, tmp);
  1376. /* record when the snapshot was created in key.offset */
  1377. key.offset = trans->transid;
  1378. ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
  1379. btrfs_tree_unlock(tmp);
  1380. free_extent_buffer(tmp);
  1381. if (ret) {
  1382. btrfs_abort_transaction(trans, ret);
  1383. goto fail;
  1384. }
  1385. /*
  1386. * insert root back/forward references
  1387. */
  1388. ret = btrfs_add_root_ref(trans, objectid,
  1389. parent_root->root_key.objectid,
  1390. btrfs_ino(BTRFS_I(parent_inode)), index,
  1391. dentry->d_name.name, dentry->d_name.len);
  1392. if (ret) {
  1393. btrfs_abort_transaction(trans, ret);
  1394. goto fail;
  1395. }
  1396. key.offset = (u64)-1;
  1397. pending->snap = btrfs_read_fs_root_no_name(fs_info, &key);
  1398. if (IS_ERR(pending->snap)) {
  1399. ret = PTR_ERR(pending->snap);
  1400. btrfs_abort_transaction(trans, ret);
  1401. goto fail;
  1402. }
  1403. ret = btrfs_reloc_post_snapshot(trans, pending);
  1404. if (ret) {
  1405. btrfs_abort_transaction(trans, ret);
  1406. goto fail;
  1407. }
  1408. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1409. if (ret) {
  1410. btrfs_abort_transaction(trans, ret);
  1411. goto fail;
  1412. }
  1413. /*
  1414. * Do special qgroup accounting for snapshot, as we do some qgroup
  1415. * snapshot hack to do fast snapshot.
  1416. * To co-operate with that hack, we do hack again.
  1417. * Or snapshot will be greatly slowed down by a subtree qgroup rescan
  1418. */
  1419. ret = qgroup_account_snapshot(trans, root, parent_root,
  1420. pending->inherit, objectid);
  1421. if (ret < 0)
  1422. goto fail;
  1423. ret = btrfs_insert_dir_item(trans, parent_root,
  1424. dentry->d_name.name, dentry->d_name.len,
  1425. BTRFS_I(parent_inode), &key,
  1426. BTRFS_FT_DIR, index);
  1427. /* We have check then name at the beginning, so it is impossible. */
  1428. BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
  1429. if (ret) {
  1430. btrfs_abort_transaction(trans, ret);
  1431. goto fail;
  1432. }
  1433. btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size +
  1434. dentry->d_name.len * 2);
  1435. parent_inode->i_mtime = parent_inode->i_ctime =
  1436. current_time(parent_inode);
  1437. ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
  1438. if (ret) {
  1439. btrfs_abort_transaction(trans, ret);
  1440. goto fail;
  1441. }
  1442. ret = btrfs_uuid_tree_add(trans, new_uuid.b, BTRFS_UUID_KEY_SUBVOL,
  1443. objectid);
  1444. if (ret) {
  1445. btrfs_abort_transaction(trans, ret);
  1446. goto fail;
  1447. }
  1448. if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
  1449. ret = btrfs_uuid_tree_add(trans, new_root_item->received_uuid,
  1450. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  1451. objectid);
  1452. if (ret && ret != -EEXIST) {
  1453. btrfs_abort_transaction(trans, ret);
  1454. goto fail;
  1455. }
  1456. }
  1457. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1458. if (ret) {
  1459. btrfs_abort_transaction(trans, ret);
  1460. goto fail;
  1461. }
  1462. fail:
  1463. pending->error = ret;
  1464. dir_item_existed:
  1465. trans->block_rsv = rsv;
  1466. trans->bytes_reserved = 0;
  1467. clear_skip_qgroup:
  1468. btrfs_clear_skip_qgroup(trans);
  1469. no_free_objectid:
  1470. kfree(new_root_item);
  1471. pending->root_item = NULL;
  1472. btrfs_free_path(path);
  1473. pending->path = NULL;
  1474. return ret;
  1475. }
  1476. /*
  1477. * create all the snapshots we've scheduled for creation
  1478. */
  1479. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans)
  1480. {
  1481. struct btrfs_pending_snapshot *pending, *next;
  1482. struct list_head *head = &trans->transaction->pending_snapshots;
  1483. int ret = 0;
  1484. list_for_each_entry_safe(pending, next, head, list) {
  1485. list_del(&pending->list);
  1486. ret = create_pending_snapshot(trans, pending);
  1487. if (ret)
  1488. break;
  1489. }
  1490. return ret;
  1491. }
  1492. static void update_super_roots(struct btrfs_fs_info *fs_info)
  1493. {
  1494. struct btrfs_root_item *root_item;
  1495. struct btrfs_super_block *super;
  1496. super = fs_info->super_copy;
  1497. root_item = &fs_info->chunk_root->root_item;
  1498. super->chunk_root = root_item->bytenr;
  1499. super->chunk_root_generation = root_item->generation;
  1500. super->chunk_root_level = root_item->level;
  1501. root_item = &fs_info->tree_root->root_item;
  1502. super->root = root_item->bytenr;
  1503. super->generation = root_item->generation;
  1504. super->root_level = root_item->level;
  1505. if (btrfs_test_opt(fs_info, SPACE_CACHE))
  1506. super->cache_generation = root_item->generation;
  1507. if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
  1508. super->uuid_tree_generation = root_item->generation;
  1509. }
  1510. int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
  1511. {
  1512. struct btrfs_transaction *trans;
  1513. int ret = 0;
  1514. spin_lock(&info->trans_lock);
  1515. trans = info->running_transaction;
  1516. if (trans)
  1517. ret = (trans->state >= TRANS_STATE_COMMIT_START);
  1518. spin_unlock(&info->trans_lock);
  1519. return ret;
  1520. }
  1521. int btrfs_transaction_blocked(struct btrfs_fs_info *info)
  1522. {
  1523. struct btrfs_transaction *trans;
  1524. int ret = 0;
  1525. spin_lock(&info->trans_lock);
  1526. trans = info->running_transaction;
  1527. if (trans)
  1528. ret = is_transaction_blocked(trans);
  1529. spin_unlock(&info->trans_lock);
  1530. return ret;
  1531. }
  1532. /*
  1533. * wait for the current transaction commit to start and block subsequent
  1534. * transaction joins
  1535. */
  1536. static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
  1537. struct btrfs_transaction *trans)
  1538. {
  1539. wait_event(fs_info->transaction_blocked_wait,
  1540. trans->state >= TRANS_STATE_COMMIT_START || trans->aborted);
  1541. }
  1542. /*
  1543. * wait for the current transaction to start and then become unblocked.
  1544. * caller holds ref.
  1545. */
  1546. static void wait_current_trans_commit_start_and_unblock(
  1547. struct btrfs_fs_info *fs_info,
  1548. struct btrfs_transaction *trans)
  1549. {
  1550. wait_event(fs_info->transaction_wait,
  1551. trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted);
  1552. }
  1553. /*
  1554. * commit transactions asynchronously. once btrfs_commit_transaction_async
  1555. * returns, any subsequent transaction will not be allowed to join.
  1556. */
  1557. struct btrfs_async_commit {
  1558. struct btrfs_trans_handle *newtrans;
  1559. struct work_struct work;
  1560. };
  1561. static void do_async_commit(struct work_struct *work)
  1562. {
  1563. struct btrfs_async_commit *ac =
  1564. container_of(work, struct btrfs_async_commit, work);
  1565. /*
  1566. * We've got freeze protection passed with the transaction.
  1567. * Tell lockdep about it.
  1568. */
  1569. if (ac->newtrans->type & __TRANS_FREEZABLE)
  1570. __sb_writers_acquired(ac->newtrans->fs_info->sb, SB_FREEZE_FS);
  1571. current->journal_info = ac->newtrans;
  1572. btrfs_commit_transaction(ac->newtrans);
  1573. kfree(ac);
  1574. }
  1575. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  1576. int wait_for_unblock)
  1577. {
  1578. struct btrfs_fs_info *fs_info = trans->fs_info;
  1579. struct btrfs_async_commit *ac;
  1580. struct btrfs_transaction *cur_trans;
  1581. ac = kmalloc(sizeof(*ac), GFP_NOFS);
  1582. if (!ac)
  1583. return -ENOMEM;
  1584. INIT_WORK(&ac->work, do_async_commit);
  1585. ac->newtrans = btrfs_join_transaction(trans->root);
  1586. if (IS_ERR(ac->newtrans)) {
  1587. int err = PTR_ERR(ac->newtrans);
  1588. kfree(ac);
  1589. return err;
  1590. }
  1591. /* take transaction reference */
  1592. cur_trans = trans->transaction;
  1593. refcount_inc(&cur_trans->use_count);
  1594. btrfs_end_transaction(trans);
  1595. /*
  1596. * Tell lockdep we've released the freeze rwsem, since the
  1597. * async commit thread will be the one to unlock it.
  1598. */
  1599. if (ac->newtrans->type & __TRANS_FREEZABLE)
  1600. __sb_writers_release(fs_info->sb, SB_FREEZE_FS);
  1601. schedule_work(&ac->work);
  1602. /* wait for transaction to start and unblock */
  1603. if (wait_for_unblock)
  1604. wait_current_trans_commit_start_and_unblock(fs_info, cur_trans);
  1605. else
  1606. wait_current_trans_commit_start(fs_info, cur_trans);
  1607. if (current->journal_info == trans)
  1608. current->journal_info = NULL;
  1609. btrfs_put_transaction(cur_trans);
  1610. return 0;
  1611. }
  1612. static void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
  1613. {
  1614. struct btrfs_fs_info *fs_info = trans->fs_info;
  1615. struct btrfs_transaction *cur_trans = trans->transaction;
  1616. DEFINE_WAIT(wait);
  1617. WARN_ON(refcount_read(&trans->use_count) > 1);
  1618. btrfs_abort_transaction(trans, err);
  1619. spin_lock(&fs_info->trans_lock);
  1620. /*
  1621. * If the transaction is removed from the list, it means this
  1622. * transaction has been committed successfully, so it is impossible
  1623. * to call the cleanup function.
  1624. */
  1625. BUG_ON(list_empty(&cur_trans->list));
  1626. list_del_init(&cur_trans->list);
  1627. if (cur_trans == fs_info->running_transaction) {
  1628. cur_trans->state = TRANS_STATE_COMMIT_DOING;
  1629. spin_unlock(&fs_info->trans_lock);
  1630. wait_event(cur_trans->writer_wait,
  1631. atomic_read(&cur_trans->num_writers) == 1);
  1632. spin_lock(&fs_info->trans_lock);
  1633. }
  1634. spin_unlock(&fs_info->trans_lock);
  1635. btrfs_cleanup_one_transaction(trans->transaction, fs_info);
  1636. spin_lock(&fs_info->trans_lock);
  1637. if (cur_trans == fs_info->running_transaction)
  1638. fs_info->running_transaction = NULL;
  1639. spin_unlock(&fs_info->trans_lock);
  1640. if (trans->type & __TRANS_FREEZABLE)
  1641. sb_end_intwrite(fs_info->sb);
  1642. btrfs_put_transaction(cur_trans);
  1643. btrfs_put_transaction(cur_trans);
  1644. trace_btrfs_transaction_commit(trans->root);
  1645. if (current->journal_info == trans)
  1646. current->journal_info = NULL;
  1647. btrfs_scrub_cancel(fs_info);
  1648. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1649. }
  1650. static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
  1651. {
  1652. /*
  1653. * We use writeback_inodes_sb here because if we used
  1654. * btrfs_start_delalloc_roots we would deadlock with fs freeze.
  1655. * Currently are holding the fs freeze lock, if we do an async flush
  1656. * we'll do btrfs_join_transaction() and deadlock because we need to
  1657. * wait for the fs freeze lock. Using the direct flushing we benefit
  1658. * from already being in a transaction and our join_transaction doesn't
  1659. * have to re-take the fs freeze lock.
  1660. */
  1661. if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
  1662. writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC);
  1663. return 0;
  1664. }
  1665. static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
  1666. {
  1667. if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
  1668. btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
  1669. }
  1670. static inline void
  1671. btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans)
  1672. {
  1673. wait_event(cur_trans->pending_wait,
  1674. atomic_read(&cur_trans->pending_ordered) == 0);
  1675. }
  1676. int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
  1677. {
  1678. struct btrfs_fs_info *fs_info = trans->fs_info;
  1679. struct btrfs_transaction *cur_trans = trans->transaction;
  1680. struct btrfs_transaction *prev_trans = NULL;
  1681. int ret;
  1682. /*
  1683. * Some places just start a transaction to commit it. We need to make
  1684. * sure that if this commit fails that the abort code actually marks the
  1685. * transaction as failed, so set trans->dirty to make the abort code do
  1686. * the right thing.
  1687. */
  1688. trans->dirty = true;
  1689. /* Stop the commit early if ->aborted is set */
  1690. if (unlikely(READ_ONCE(cur_trans->aborted))) {
  1691. ret = cur_trans->aborted;
  1692. btrfs_end_transaction(trans);
  1693. return ret;
  1694. }
  1695. btrfs_trans_release_metadata(trans);
  1696. trans->block_rsv = NULL;
  1697. /* make a pass through all the delayed refs we have so far
  1698. * any runnings procs may add more while we are here
  1699. */
  1700. ret = btrfs_run_delayed_refs(trans, 0);
  1701. if (ret) {
  1702. btrfs_end_transaction(trans);
  1703. return ret;
  1704. }
  1705. cur_trans = trans->transaction;
  1706. /*
  1707. * set the flushing flag so procs in this transaction have to
  1708. * start sending their work down.
  1709. */
  1710. cur_trans->delayed_refs.flushing = 1;
  1711. smp_wmb();
  1712. if (!list_empty(&trans->new_bgs))
  1713. btrfs_create_pending_block_groups(trans);
  1714. ret = btrfs_run_delayed_refs(trans, 0);
  1715. if (ret) {
  1716. btrfs_end_transaction(trans);
  1717. return ret;
  1718. }
  1719. if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
  1720. int run_it = 0;
  1721. /* this mutex is also taken before trying to set
  1722. * block groups readonly. We need to make sure
  1723. * that nobody has set a block group readonly
  1724. * after a extents from that block group have been
  1725. * allocated for cache files. btrfs_set_block_group_ro
  1726. * will wait for the transaction to commit if it
  1727. * finds BTRFS_TRANS_DIRTY_BG_RUN set.
  1728. *
  1729. * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
  1730. * only one process starts all the block group IO. It wouldn't
  1731. * hurt to have more than one go through, but there's no
  1732. * real advantage to it either.
  1733. */
  1734. mutex_lock(&fs_info->ro_block_group_mutex);
  1735. if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
  1736. &cur_trans->flags))
  1737. run_it = 1;
  1738. mutex_unlock(&fs_info->ro_block_group_mutex);
  1739. if (run_it) {
  1740. ret = btrfs_start_dirty_block_groups(trans);
  1741. if (ret) {
  1742. btrfs_end_transaction(trans);
  1743. return ret;
  1744. }
  1745. }
  1746. }
  1747. spin_lock(&fs_info->trans_lock);
  1748. if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
  1749. spin_unlock(&fs_info->trans_lock);
  1750. refcount_inc(&cur_trans->use_count);
  1751. ret = btrfs_end_transaction(trans);
  1752. wait_for_commit(cur_trans);
  1753. if (unlikely(cur_trans->aborted))
  1754. ret = cur_trans->aborted;
  1755. btrfs_put_transaction(cur_trans);
  1756. return ret;
  1757. }
  1758. cur_trans->state = TRANS_STATE_COMMIT_START;
  1759. wake_up(&fs_info->transaction_blocked_wait);
  1760. if (cur_trans->list.prev != &fs_info->trans_list) {
  1761. prev_trans = list_entry(cur_trans->list.prev,
  1762. struct btrfs_transaction, list);
  1763. if (prev_trans->state != TRANS_STATE_COMPLETED) {
  1764. refcount_inc(&prev_trans->use_count);
  1765. spin_unlock(&fs_info->trans_lock);
  1766. wait_for_commit(prev_trans);
  1767. ret = prev_trans->aborted;
  1768. btrfs_put_transaction(prev_trans);
  1769. if (ret)
  1770. goto cleanup_transaction;
  1771. } else {
  1772. spin_unlock(&fs_info->trans_lock);
  1773. }
  1774. } else {
  1775. spin_unlock(&fs_info->trans_lock);
  1776. /*
  1777. * The previous transaction was aborted and was already removed
  1778. * from the list of transactions at fs_info->trans_list. So we
  1779. * abort to prevent writing a new superblock that reflects a
  1780. * corrupt state (pointing to trees with unwritten nodes/leafs).
  1781. */
  1782. if (test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) {
  1783. ret = -EROFS;
  1784. goto cleanup_transaction;
  1785. }
  1786. }
  1787. extwriter_counter_dec(cur_trans, trans->type);
  1788. ret = btrfs_start_delalloc_flush(fs_info);
  1789. if (ret)
  1790. goto cleanup_transaction;
  1791. ret = btrfs_run_delayed_items(trans);
  1792. if (ret)
  1793. goto cleanup_transaction;
  1794. wait_event(cur_trans->writer_wait,
  1795. extwriter_counter_read(cur_trans) == 0);
  1796. /* some pending stuffs might be added after the previous flush. */
  1797. ret = btrfs_run_delayed_items(trans);
  1798. if (ret)
  1799. goto cleanup_transaction;
  1800. btrfs_wait_delalloc_flush(fs_info);
  1801. btrfs_wait_pending_ordered(cur_trans);
  1802. btrfs_scrub_pause(fs_info);
  1803. /*
  1804. * Ok now we need to make sure to block out any other joins while we
  1805. * commit the transaction. We could have started a join before setting
  1806. * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
  1807. */
  1808. spin_lock(&fs_info->trans_lock);
  1809. cur_trans->state = TRANS_STATE_COMMIT_DOING;
  1810. spin_unlock(&fs_info->trans_lock);
  1811. wait_event(cur_trans->writer_wait,
  1812. atomic_read(&cur_trans->num_writers) == 1);
  1813. /* ->aborted might be set after the previous check, so check it */
  1814. if (unlikely(READ_ONCE(cur_trans->aborted))) {
  1815. ret = cur_trans->aborted;
  1816. goto scrub_continue;
  1817. }
  1818. /*
  1819. * the reloc mutex makes sure that we stop
  1820. * the balancing code from coming in and moving
  1821. * extents around in the middle of the commit
  1822. */
  1823. mutex_lock(&fs_info->reloc_mutex);
  1824. /*
  1825. * We needn't worry about the delayed items because we will
  1826. * deal with them in create_pending_snapshot(), which is the
  1827. * core function of the snapshot creation.
  1828. */
  1829. ret = create_pending_snapshots(trans);
  1830. if (ret) {
  1831. mutex_unlock(&fs_info->reloc_mutex);
  1832. goto scrub_continue;
  1833. }
  1834. /*
  1835. * We insert the dir indexes of the snapshots and update the inode
  1836. * of the snapshots' parents after the snapshot creation, so there
  1837. * are some delayed items which are not dealt with. Now deal with
  1838. * them.
  1839. *
  1840. * We needn't worry that this operation will corrupt the snapshots,
  1841. * because all the tree which are snapshoted will be forced to COW
  1842. * the nodes and leaves.
  1843. */
  1844. ret = btrfs_run_delayed_items(trans);
  1845. if (ret) {
  1846. mutex_unlock(&fs_info->reloc_mutex);
  1847. goto scrub_continue;
  1848. }
  1849. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1850. if (ret) {
  1851. mutex_unlock(&fs_info->reloc_mutex);
  1852. goto scrub_continue;
  1853. }
  1854. /*
  1855. * make sure none of the code above managed to slip in a
  1856. * delayed item
  1857. */
  1858. btrfs_assert_delayed_root_empty(fs_info);
  1859. WARN_ON(cur_trans != trans->transaction);
  1860. /* btrfs_commit_tree_roots is responsible for getting the
  1861. * various roots consistent with each other. Every pointer
  1862. * in the tree of tree roots has to point to the most up to date
  1863. * root for every subvolume and other tree. So, we have to keep
  1864. * the tree logging code from jumping in and changing any
  1865. * of the trees.
  1866. *
  1867. * At this point in the commit, there can't be any tree-log
  1868. * writers, but a little lower down we drop the trans mutex
  1869. * and let new people in. By holding the tree_log_mutex
  1870. * from now until after the super is written, we avoid races
  1871. * with the tree-log code.
  1872. */
  1873. mutex_lock(&fs_info->tree_log_mutex);
  1874. ret = commit_fs_roots(trans);
  1875. if (ret) {
  1876. mutex_unlock(&fs_info->tree_log_mutex);
  1877. mutex_unlock(&fs_info->reloc_mutex);
  1878. goto scrub_continue;
  1879. }
  1880. /*
  1881. * Since the transaction is done, we can apply the pending changes
  1882. * before the next transaction.
  1883. */
  1884. btrfs_apply_pending_changes(fs_info);
  1885. /* commit_fs_roots gets rid of all the tree log roots, it is now
  1886. * safe to free the root of tree log roots
  1887. */
  1888. btrfs_free_log_root_tree(trans, fs_info);
  1889. /*
  1890. * commit_fs_roots() can call btrfs_save_ino_cache(), which generates
  1891. * new delayed refs. Must handle them or qgroup can be wrong.
  1892. */
  1893. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1894. if (ret) {
  1895. mutex_unlock(&fs_info->tree_log_mutex);
  1896. mutex_unlock(&fs_info->reloc_mutex);
  1897. goto scrub_continue;
  1898. }
  1899. /*
  1900. * Since fs roots are all committed, we can get a quite accurate
  1901. * new_roots. So let's do quota accounting.
  1902. */
  1903. ret = btrfs_qgroup_account_extents(trans);
  1904. if (ret < 0) {
  1905. mutex_unlock(&fs_info->tree_log_mutex);
  1906. mutex_unlock(&fs_info->reloc_mutex);
  1907. goto scrub_continue;
  1908. }
  1909. ret = commit_cowonly_roots(trans);
  1910. if (ret) {
  1911. mutex_unlock(&fs_info->tree_log_mutex);
  1912. mutex_unlock(&fs_info->reloc_mutex);
  1913. goto scrub_continue;
  1914. }
  1915. /*
  1916. * The tasks which save the space cache and inode cache may also
  1917. * update ->aborted, check it.
  1918. */
  1919. if (unlikely(READ_ONCE(cur_trans->aborted))) {
  1920. ret = cur_trans->aborted;
  1921. mutex_unlock(&fs_info->tree_log_mutex);
  1922. mutex_unlock(&fs_info->reloc_mutex);
  1923. goto scrub_continue;
  1924. }
  1925. btrfs_prepare_extent_commit(fs_info);
  1926. cur_trans = fs_info->running_transaction;
  1927. btrfs_set_root_node(&fs_info->tree_root->root_item,
  1928. fs_info->tree_root->node);
  1929. list_add_tail(&fs_info->tree_root->dirty_list,
  1930. &cur_trans->switch_commits);
  1931. btrfs_set_root_node(&fs_info->chunk_root->root_item,
  1932. fs_info->chunk_root->node);
  1933. list_add_tail(&fs_info->chunk_root->dirty_list,
  1934. &cur_trans->switch_commits);
  1935. switch_commit_roots(cur_trans);
  1936. ASSERT(list_empty(&cur_trans->dirty_bgs));
  1937. ASSERT(list_empty(&cur_trans->io_bgs));
  1938. update_super_roots(fs_info);
  1939. btrfs_set_super_log_root(fs_info->super_copy, 0);
  1940. btrfs_set_super_log_root_level(fs_info->super_copy, 0);
  1941. memcpy(fs_info->super_for_commit, fs_info->super_copy,
  1942. sizeof(*fs_info->super_copy));
  1943. btrfs_update_commit_device_size(fs_info);
  1944. btrfs_update_commit_device_bytes_used(cur_trans);
  1945. clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
  1946. clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
  1947. btrfs_trans_release_chunk_metadata(trans);
  1948. spin_lock(&fs_info->trans_lock);
  1949. cur_trans->state = TRANS_STATE_UNBLOCKED;
  1950. fs_info->running_transaction = NULL;
  1951. spin_unlock(&fs_info->trans_lock);
  1952. mutex_unlock(&fs_info->reloc_mutex);
  1953. wake_up(&fs_info->transaction_wait);
  1954. ret = btrfs_write_and_wait_transaction(trans);
  1955. if (ret) {
  1956. btrfs_handle_fs_error(fs_info, ret,
  1957. "Error while writing out transaction");
  1958. mutex_unlock(&fs_info->tree_log_mutex);
  1959. goto scrub_continue;
  1960. }
  1961. ret = write_all_supers(fs_info, 0);
  1962. /*
  1963. * the super is written, we can safely allow the tree-loggers
  1964. * to go about their business
  1965. */
  1966. mutex_unlock(&fs_info->tree_log_mutex);
  1967. if (ret)
  1968. goto scrub_continue;
  1969. btrfs_finish_extent_commit(trans);
  1970. if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
  1971. btrfs_clear_space_info_full(fs_info);
  1972. fs_info->last_trans_committed = cur_trans->transid;
  1973. /*
  1974. * We needn't acquire the lock here because there is no other task
  1975. * which can change it.
  1976. */
  1977. cur_trans->state = TRANS_STATE_COMPLETED;
  1978. wake_up(&cur_trans->commit_wait);
  1979. clear_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags);
  1980. spin_lock(&fs_info->trans_lock);
  1981. list_del_init(&cur_trans->list);
  1982. spin_unlock(&fs_info->trans_lock);
  1983. btrfs_put_transaction(cur_trans);
  1984. btrfs_put_transaction(cur_trans);
  1985. if (trans->type & __TRANS_FREEZABLE)
  1986. sb_end_intwrite(fs_info->sb);
  1987. trace_btrfs_transaction_commit(trans->root);
  1988. btrfs_scrub_continue(fs_info);
  1989. if (current->journal_info == trans)
  1990. current->journal_info = NULL;
  1991. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1992. return ret;
  1993. scrub_continue:
  1994. btrfs_scrub_continue(fs_info);
  1995. cleanup_transaction:
  1996. btrfs_trans_release_metadata(trans);
  1997. btrfs_trans_release_chunk_metadata(trans);
  1998. trans->block_rsv = NULL;
  1999. btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
  2000. if (current->journal_info == trans)
  2001. current->journal_info = NULL;
  2002. cleanup_transaction(trans, ret);
  2003. return ret;
  2004. }
  2005. /*
  2006. * return < 0 if error
  2007. * 0 if there are no more dead_roots at the time of call
  2008. * 1 there are more to be processed, call me again
  2009. *
  2010. * The return value indicates there are certainly more snapshots to delete, but
  2011. * if there comes a new one during processing, it may return 0. We don't mind,
  2012. * because btrfs_commit_super will poke cleaner thread and it will process it a
  2013. * few seconds later.
  2014. */
  2015. int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
  2016. {
  2017. int ret;
  2018. struct btrfs_fs_info *fs_info = root->fs_info;
  2019. spin_lock(&fs_info->trans_lock);
  2020. if (list_empty(&fs_info->dead_roots)) {
  2021. spin_unlock(&fs_info->trans_lock);
  2022. return 0;
  2023. }
  2024. root = list_first_entry(&fs_info->dead_roots,
  2025. struct btrfs_root, root_list);
  2026. list_del_init(&root->root_list);
  2027. spin_unlock(&fs_info->trans_lock);
  2028. btrfs_debug(fs_info, "cleaner removing %llu", root->objectid);
  2029. btrfs_kill_all_delayed_nodes(root);
  2030. if (btrfs_header_backref_rev(root->node) <
  2031. BTRFS_MIXED_BACKREF_REV)
  2032. ret = btrfs_drop_snapshot(root, NULL, 0, 0);
  2033. else
  2034. ret = btrfs_drop_snapshot(root, NULL, 1, 0);
  2035. return (ret < 0) ? 0 : 1;
  2036. }
  2037. void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info)
  2038. {
  2039. unsigned long prev;
  2040. unsigned long bit;
  2041. prev = xchg(&fs_info->pending_changes, 0);
  2042. if (!prev)
  2043. return;
  2044. bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE;
  2045. if (prev & bit)
  2046. btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE);
  2047. prev &= ~bit;
  2048. bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE;
  2049. if (prev & bit)
  2050. btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE);
  2051. prev &= ~bit;
  2052. bit = 1 << BTRFS_PENDING_COMMIT;
  2053. if (prev & bit)
  2054. btrfs_debug(fs_info, "pending commit done");
  2055. prev &= ~bit;
  2056. if (prev)
  2057. btrfs_warn(fs_info,
  2058. "unknown pending changes left 0x%lx, ignoring", prev);
  2059. }