transaction.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397
  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. btrfs_record_root_in_trans(h, root);
  503. if (!current->journal_info)
  504. current->journal_info = h;
  505. return h;
  506. join_fail:
  507. if (type & __TRANS_FREEZABLE)
  508. sb_end_intwrite(fs_info->sb);
  509. kmem_cache_free(btrfs_trans_handle_cachep, h);
  510. alloc_fail:
  511. if (num_bytes)
  512. btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv,
  513. num_bytes);
  514. reserve_fail:
  515. btrfs_qgroup_free_meta_pertrans(root, qgroup_reserved);
  516. return ERR_PTR(ret);
  517. }
  518. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  519. unsigned int num_items)
  520. {
  521. return start_transaction(root, num_items, TRANS_START,
  522. BTRFS_RESERVE_FLUSH_ALL, true);
  523. }
  524. struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
  525. struct btrfs_root *root,
  526. unsigned int num_items,
  527. int min_factor)
  528. {
  529. struct btrfs_fs_info *fs_info = root->fs_info;
  530. struct btrfs_trans_handle *trans;
  531. u64 num_bytes;
  532. int ret;
  533. /*
  534. * We have two callers: unlink and block group removal. The
  535. * former should succeed even if we will temporarily exceed
  536. * quota and the latter operates on the extent root so
  537. * qgroup enforcement is ignored anyway.
  538. */
  539. trans = start_transaction(root, num_items, TRANS_START,
  540. BTRFS_RESERVE_FLUSH_ALL, false);
  541. if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
  542. return trans;
  543. trans = btrfs_start_transaction(root, 0);
  544. if (IS_ERR(trans))
  545. return trans;
  546. num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items);
  547. ret = btrfs_cond_migrate_bytes(fs_info, &fs_info->trans_block_rsv,
  548. num_bytes, min_factor);
  549. if (ret) {
  550. btrfs_end_transaction(trans);
  551. return ERR_PTR(ret);
  552. }
  553. trans->block_rsv = &fs_info->trans_block_rsv;
  554. trans->bytes_reserved = num_bytes;
  555. trace_btrfs_space_reservation(fs_info, "transaction",
  556. trans->transid, num_bytes, 1);
  557. return trans;
  558. }
  559. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
  560. {
  561. return start_transaction(root, 0, TRANS_JOIN, BTRFS_RESERVE_NO_FLUSH,
  562. true);
  563. }
  564. struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
  565. {
  566. return start_transaction(root, 0, TRANS_JOIN_NOLOCK,
  567. BTRFS_RESERVE_NO_FLUSH, true);
  568. }
  569. /*
  570. * Similar to regular join but it never starts a transaction when none is
  571. * running or after waiting for the current one to finish.
  572. */
  573. struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *root)
  574. {
  575. return start_transaction(root, 0, TRANS_JOIN_NOSTART,
  576. BTRFS_RESERVE_NO_FLUSH, true);
  577. }
  578. /*
  579. * btrfs_attach_transaction() - catch the running transaction
  580. *
  581. * It is used when we want to commit the current the transaction, but
  582. * don't want to start a new one.
  583. *
  584. * Note: If this function return -ENOENT, it just means there is no
  585. * running transaction. But it is possible that the inactive transaction
  586. * is still in the memory, not fully on disk. If you hope there is no
  587. * inactive transaction in the fs when -ENOENT is returned, you should
  588. * invoke
  589. * btrfs_attach_transaction_barrier()
  590. */
  591. struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root)
  592. {
  593. return start_transaction(root, 0, TRANS_ATTACH,
  594. BTRFS_RESERVE_NO_FLUSH, true);
  595. }
  596. /*
  597. * btrfs_attach_transaction_barrier() - catch the running transaction
  598. *
  599. * It is similar to the above function, the differentia is this one
  600. * will wait for all the inactive transactions until they fully
  601. * complete.
  602. */
  603. struct btrfs_trans_handle *
  604. btrfs_attach_transaction_barrier(struct btrfs_root *root)
  605. {
  606. struct btrfs_trans_handle *trans;
  607. trans = start_transaction(root, 0, TRANS_ATTACH,
  608. BTRFS_RESERVE_NO_FLUSH, true);
  609. if (trans == ERR_PTR(-ENOENT))
  610. btrfs_wait_for_commit(root->fs_info, 0);
  611. return trans;
  612. }
  613. /* wait for a transaction commit to be fully complete */
  614. static noinline void wait_for_commit(struct btrfs_transaction *commit)
  615. {
  616. wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
  617. }
  618. int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
  619. {
  620. struct btrfs_transaction *cur_trans = NULL, *t;
  621. int ret = 0;
  622. if (transid) {
  623. if (transid <= fs_info->last_trans_committed)
  624. goto out;
  625. /* find specified transaction */
  626. spin_lock(&fs_info->trans_lock);
  627. list_for_each_entry(t, &fs_info->trans_list, list) {
  628. if (t->transid == transid) {
  629. cur_trans = t;
  630. refcount_inc(&cur_trans->use_count);
  631. ret = 0;
  632. break;
  633. }
  634. if (t->transid > transid) {
  635. ret = 0;
  636. break;
  637. }
  638. }
  639. spin_unlock(&fs_info->trans_lock);
  640. /*
  641. * The specified transaction doesn't exist, or we
  642. * raced with btrfs_commit_transaction
  643. */
  644. if (!cur_trans) {
  645. if (transid > fs_info->last_trans_committed)
  646. ret = -EINVAL;
  647. goto out;
  648. }
  649. } else {
  650. /* find newest transaction that is committing | committed */
  651. spin_lock(&fs_info->trans_lock);
  652. list_for_each_entry_reverse(t, &fs_info->trans_list,
  653. list) {
  654. if (t->state >= TRANS_STATE_COMMIT_START) {
  655. if (t->state == TRANS_STATE_COMPLETED)
  656. break;
  657. cur_trans = t;
  658. refcount_inc(&cur_trans->use_count);
  659. break;
  660. }
  661. }
  662. spin_unlock(&fs_info->trans_lock);
  663. if (!cur_trans)
  664. goto out; /* nothing committing|committed */
  665. }
  666. wait_for_commit(cur_trans);
  667. btrfs_put_transaction(cur_trans);
  668. out:
  669. return ret;
  670. }
  671. void btrfs_throttle(struct btrfs_fs_info *fs_info)
  672. {
  673. wait_current_trans(fs_info);
  674. }
  675. static int should_end_transaction(struct btrfs_trans_handle *trans)
  676. {
  677. struct btrfs_fs_info *fs_info = trans->fs_info;
  678. if (btrfs_check_space_for_delayed_refs(trans, fs_info))
  679. return 1;
  680. return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5);
  681. }
  682. int btrfs_should_end_transaction(struct btrfs_trans_handle *trans)
  683. {
  684. struct btrfs_transaction *cur_trans = trans->transaction;
  685. int updates;
  686. int err;
  687. smp_mb();
  688. if (cur_trans->state >= TRANS_STATE_BLOCKED ||
  689. cur_trans->delayed_refs.flushing)
  690. return 1;
  691. updates = trans->delayed_ref_updates;
  692. trans->delayed_ref_updates = 0;
  693. if (updates) {
  694. err = btrfs_run_delayed_refs(trans, updates * 2);
  695. if (err) /* Error code will also eval true */
  696. return err;
  697. }
  698. return should_end_transaction(trans);
  699. }
  700. static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans)
  701. {
  702. struct btrfs_fs_info *fs_info = trans->fs_info;
  703. if (!trans->block_rsv) {
  704. ASSERT(!trans->bytes_reserved);
  705. return;
  706. }
  707. if (!trans->bytes_reserved)
  708. return;
  709. ASSERT(trans->block_rsv == &fs_info->trans_block_rsv);
  710. trace_btrfs_space_reservation(fs_info, "transaction",
  711. trans->transid, trans->bytes_reserved, 0);
  712. btrfs_block_rsv_release(fs_info, trans->block_rsv,
  713. trans->bytes_reserved);
  714. trans->bytes_reserved = 0;
  715. }
  716. static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
  717. int throttle)
  718. {
  719. struct btrfs_fs_info *info = trans->fs_info;
  720. struct btrfs_transaction *cur_trans = trans->transaction;
  721. u64 transid = trans->transid;
  722. unsigned long cur = trans->delayed_ref_updates;
  723. int lock = (trans->type != TRANS_JOIN_NOLOCK);
  724. int err = 0;
  725. int must_run_delayed_refs = 0;
  726. if (refcount_read(&trans->use_count) > 1) {
  727. refcount_dec(&trans->use_count);
  728. trans->block_rsv = trans->orig_rsv;
  729. return 0;
  730. }
  731. btrfs_trans_release_metadata(trans);
  732. trans->block_rsv = NULL;
  733. if (!list_empty(&trans->new_bgs))
  734. btrfs_create_pending_block_groups(trans);
  735. trans->delayed_ref_updates = 0;
  736. if (!trans->sync) {
  737. must_run_delayed_refs =
  738. btrfs_should_throttle_delayed_refs(trans, info);
  739. cur = max_t(unsigned long, cur, 32);
  740. /*
  741. * don't make the caller wait if they are from a NOLOCK
  742. * or ATTACH transaction, it will deadlock with commit
  743. */
  744. if (must_run_delayed_refs == 1 &&
  745. (trans->type & (__TRANS_JOIN_NOLOCK | __TRANS_ATTACH)))
  746. must_run_delayed_refs = 2;
  747. }
  748. btrfs_trans_release_metadata(trans);
  749. trans->block_rsv = NULL;
  750. if (!list_empty(&trans->new_bgs))
  751. btrfs_create_pending_block_groups(trans);
  752. btrfs_trans_release_chunk_metadata(trans);
  753. if (lock && should_end_transaction(trans) &&
  754. READ_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
  755. spin_lock(&info->trans_lock);
  756. if (cur_trans->state == TRANS_STATE_RUNNING)
  757. cur_trans->state = TRANS_STATE_BLOCKED;
  758. spin_unlock(&info->trans_lock);
  759. }
  760. if (lock && READ_ONCE(cur_trans->state) == TRANS_STATE_BLOCKED) {
  761. if (throttle)
  762. return btrfs_commit_transaction(trans);
  763. else
  764. wake_up_process(info->transaction_kthread);
  765. }
  766. if (trans->type & __TRANS_FREEZABLE)
  767. sb_end_intwrite(info->sb);
  768. WARN_ON(cur_trans != info->running_transaction);
  769. WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
  770. atomic_dec(&cur_trans->num_writers);
  771. extwriter_counter_dec(cur_trans, trans->type);
  772. cond_wake_up(&cur_trans->writer_wait);
  773. btrfs_put_transaction(cur_trans);
  774. if (current->journal_info == trans)
  775. current->journal_info = NULL;
  776. if (throttle)
  777. btrfs_run_delayed_iputs(info);
  778. if (trans->aborted ||
  779. test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) {
  780. wake_up_process(info->transaction_kthread);
  781. err = -EIO;
  782. }
  783. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  784. if (must_run_delayed_refs) {
  785. btrfs_async_run_delayed_refs(info, cur, transid,
  786. must_run_delayed_refs == 1);
  787. }
  788. return err;
  789. }
  790. int btrfs_end_transaction(struct btrfs_trans_handle *trans)
  791. {
  792. return __btrfs_end_transaction(trans, 0);
  793. }
  794. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans)
  795. {
  796. return __btrfs_end_transaction(trans, 1);
  797. }
  798. /*
  799. * when btree blocks are allocated, they have some corresponding bits set for
  800. * them in one of two extent_io trees. This is used to make sure all of
  801. * those extents are sent to disk but does not wait on them
  802. */
  803. int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
  804. struct extent_io_tree *dirty_pages, int mark)
  805. {
  806. int err = 0;
  807. int werr = 0;
  808. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  809. struct extent_state *cached_state = NULL;
  810. u64 start = 0;
  811. u64 end;
  812. atomic_inc(&BTRFS_I(fs_info->btree_inode)->sync_writers);
  813. while (!find_first_extent_bit(dirty_pages, start, &start, &end,
  814. mark, &cached_state)) {
  815. bool wait_writeback = false;
  816. err = convert_extent_bit(dirty_pages, start, end,
  817. EXTENT_NEED_WAIT,
  818. mark, &cached_state);
  819. /*
  820. * convert_extent_bit can return -ENOMEM, which is most of the
  821. * time a temporary error. So when it happens, ignore the error
  822. * and wait for writeback of this range to finish - because we
  823. * failed to set the bit EXTENT_NEED_WAIT for the range, a call
  824. * to __btrfs_wait_marked_extents() would not know that
  825. * writeback for this range started and therefore wouldn't
  826. * wait for it to finish - we don't want to commit a
  827. * superblock that points to btree nodes/leafs for which
  828. * writeback hasn't finished yet (and without errors).
  829. * We cleanup any entries left in the io tree when committing
  830. * the transaction (through clear_btree_io_tree()).
  831. */
  832. if (err == -ENOMEM) {
  833. err = 0;
  834. wait_writeback = true;
  835. }
  836. if (!err)
  837. err = filemap_fdatawrite_range(mapping, start, end);
  838. if (err)
  839. werr = err;
  840. else if (wait_writeback)
  841. werr = filemap_fdatawait_range(mapping, start, end);
  842. free_extent_state(cached_state);
  843. cached_state = NULL;
  844. cond_resched();
  845. start = end + 1;
  846. }
  847. atomic_dec(&BTRFS_I(fs_info->btree_inode)->sync_writers);
  848. return werr;
  849. }
  850. /*
  851. * when btree blocks are allocated, they have some corresponding bits set for
  852. * them in one of two extent_io trees. This is used to make sure all of
  853. * those extents are on disk for transaction or log commit. We wait
  854. * on all the pages and clear them from the dirty pages state tree
  855. */
  856. static int __btrfs_wait_marked_extents(struct btrfs_fs_info *fs_info,
  857. struct extent_io_tree *dirty_pages)
  858. {
  859. int err = 0;
  860. int werr = 0;
  861. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  862. struct extent_state *cached_state = NULL;
  863. u64 start = 0;
  864. u64 end;
  865. while (!find_first_extent_bit(dirty_pages, start, &start, &end,
  866. EXTENT_NEED_WAIT, &cached_state)) {
  867. /*
  868. * Ignore -ENOMEM errors returned by clear_extent_bit().
  869. * When committing the transaction, we'll remove any entries
  870. * left in the io tree. For a log commit, we don't remove them
  871. * after committing the log because the tree can be accessed
  872. * concurrently - we do it only at transaction commit time when
  873. * it's safe to do it (through clear_btree_io_tree()).
  874. */
  875. err = clear_extent_bit(dirty_pages, start, end,
  876. EXTENT_NEED_WAIT, 0, 0, &cached_state);
  877. if (err == -ENOMEM)
  878. err = 0;
  879. if (!err)
  880. err = filemap_fdatawait_range(mapping, start, end);
  881. if (err)
  882. werr = err;
  883. free_extent_state(cached_state);
  884. cached_state = NULL;
  885. cond_resched();
  886. start = end + 1;
  887. }
  888. if (err)
  889. werr = err;
  890. return werr;
  891. }
  892. int btrfs_wait_extents(struct btrfs_fs_info *fs_info,
  893. struct extent_io_tree *dirty_pages)
  894. {
  895. bool errors = false;
  896. int err;
  897. err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
  898. if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags))
  899. errors = true;
  900. if (errors && !err)
  901. err = -EIO;
  902. return err;
  903. }
  904. int btrfs_wait_tree_log_extents(struct btrfs_root *log_root, int mark)
  905. {
  906. struct btrfs_fs_info *fs_info = log_root->fs_info;
  907. struct extent_io_tree *dirty_pages = &log_root->dirty_log_pages;
  908. bool errors = false;
  909. int err;
  910. ASSERT(log_root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
  911. err = __btrfs_wait_marked_extents(fs_info, dirty_pages);
  912. if ((mark & EXTENT_DIRTY) &&
  913. test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags))
  914. errors = true;
  915. if ((mark & EXTENT_NEW) &&
  916. test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags))
  917. errors = true;
  918. if (errors && !err)
  919. err = -EIO;
  920. return err;
  921. }
  922. /*
  923. * When btree blocks are allocated the corresponding extents are marked dirty.
  924. * This function ensures such extents are persisted on disk for transaction or
  925. * log commit.
  926. *
  927. * @trans: transaction whose dirty pages we'd like to write
  928. */
  929. static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans)
  930. {
  931. int ret;
  932. int ret2;
  933. struct extent_io_tree *dirty_pages = &trans->transaction->dirty_pages;
  934. struct btrfs_fs_info *fs_info = trans->fs_info;
  935. struct blk_plug plug;
  936. blk_start_plug(&plug);
  937. ret = btrfs_write_marked_extents(fs_info, dirty_pages, EXTENT_DIRTY);
  938. blk_finish_plug(&plug);
  939. ret2 = btrfs_wait_extents(fs_info, dirty_pages);
  940. clear_btree_io_tree(&trans->transaction->dirty_pages);
  941. if (ret)
  942. return ret;
  943. else if (ret2)
  944. return ret2;
  945. else
  946. return 0;
  947. }
  948. /*
  949. * this is used to update the root pointer in the tree of tree roots.
  950. *
  951. * But, in the case of the extent allocation tree, updating the root
  952. * pointer may allocate blocks which may change the root of the extent
  953. * allocation tree.
  954. *
  955. * So, this loops and repeats and makes sure the cowonly root didn't
  956. * change while the root pointer was being updated in the metadata.
  957. */
  958. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  959. struct btrfs_root *root)
  960. {
  961. int ret;
  962. u64 old_root_bytenr;
  963. u64 old_root_used;
  964. struct btrfs_fs_info *fs_info = root->fs_info;
  965. struct btrfs_root *tree_root = fs_info->tree_root;
  966. old_root_used = btrfs_root_used(&root->root_item);
  967. while (1) {
  968. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  969. if (old_root_bytenr == root->node->start &&
  970. old_root_used == btrfs_root_used(&root->root_item))
  971. break;
  972. btrfs_set_root_node(&root->root_item, root->node);
  973. ret = btrfs_update_root(trans, tree_root,
  974. &root->root_key,
  975. &root->root_item);
  976. if (ret)
  977. return ret;
  978. old_root_used = btrfs_root_used(&root->root_item);
  979. }
  980. return 0;
  981. }
  982. /*
  983. * update all the cowonly tree roots on disk
  984. *
  985. * The error handling in this function may not be obvious. Any of the
  986. * failures will cause the file system to go offline. We still need
  987. * to clean up the delayed refs.
  988. */
  989. static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
  990. {
  991. struct btrfs_fs_info *fs_info = trans->fs_info;
  992. struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
  993. struct list_head *io_bgs = &trans->transaction->io_bgs;
  994. struct list_head *next;
  995. struct extent_buffer *eb;
  996. int ret;
  997. eb = btrfs_lock_root_node(fs_info->tree_root);
  998. ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL,
  999. 0, &eb);
  1000. btrfs_tree_unlock(eb);
  1001. free_extent_buffer(eb);
  1002. if (ret)
  1003. return ret;
  1004. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1005. if (ret)
  1006. return ret;
  1007. ret = btrfs_run_dev_stats(trans, fs_info);
  1008. if (ret)
  1009. return ret;
  1010. ret = btrfs_run_dev_replace(trans, fs_info);
  1011. if (ret)
  1012. return ret;
  1013. ret = btrfs_run_qgroups(trans);
  1014. if (ret)
  1015. return ret;
  1016. ret = btrfs_setup_space_cache(trans, fs_info);
  1017. if (ret)
  1018. return ret;
  1019. /* run_qgroups might have added some more refs */
  1020. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1021. if (ret)
  1022. return ret;
  1023. again:
  1024. while (!list_empty(&fs_info->dirty_cowonly_roots)) {
  1025. struct btrfs_root *root;
  1026. next = fs_info->dirty_cowonly_roots.next;
  1027. list_del_init(next);
  1028. root = list_entry(next, struct btrfs_root, dirty_list);
  1029. clear_bit(BTRFS_ROOT_DIRTY, &root->state);
  1030. if (root != fs_info->extent_root)
  1031. list_add_tail(&root->dirty_list,
  1032. &trans->transaction->switch_commits);
  1033. ret = update_cowonly_root(trans, root);
  1034. if (ret)
  1035. return ret;
  1036. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1037. if (ret)
  1038. return ret;
  1039. }
  1040. while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
  1041. ret = btrfs_write_dirty_block_groups(trans, fs_info);
  1042. if (ret)
  1043. return ret;
  1044. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1045. if (ret)
  1046. return ret;
  1047. }
  1048. if (!list_empty(&fs_info->dirty_cowonly_roots))
  1049. goto again;
  1050. list_add_tail(&fs_info->extent_root->dirty_list,
  1051. &trans->transaction->switch_commits);
  1052. btrfs_after_dev_replace_commit(fs_info);
  1053. return 0;
  1054. }
  1055. /*
  1056. * dead roots are old snapshots that need to be deleted. This allocates
  1057. * a dirty root struct and adds it into the list of dead roots that need to
  1058. * be deleted
  1059. */
  1060. void btrfs_add_dead_root(struct btrfs_root *root)
  1061. {
  1062. struct btrfs_fs_info *fs_info = root->fs_info;
  1063. spin_lock(&fs_info->trans_lock);
  1064. if (list_empty(&root->root_list))
  1065. list_add_tail(&root->root_list, &fs_info->dead_roots);
  1066. spin_unlock(&fs_info->trans_lock);
  1067. }
  1068. /*
  1069. * update all the cowonly tree roots on disk
  1070. */
  1071. static noinline int commit_fs_roots(struct btrfs_trans_handle *trans)
  1072. {
  1073. struct btrfs_fs_info *fs_info = trans->fs_info;
  1074. struct btrfs_root *gang[8];
  1075. int i;
  1076. int ret;
  1077. int err = 0;
  1078. spin_lock(&fs_info->fs_roots_radix_lock);
  1079. while (1) {
  1080. ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
  1081. (void **)gang, 0,
  1082. ARRAY_SIZE(gang),
  1083. BTRFS_ROOT_TRANS_TAG);
  1084. if (ret == 0)
  1085. break;
  1086. for (i = 0; i < ret; i++) {
  1087. struct btrfs_root *root = gang[i];
  1088. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  1089. (unsigned long)root->root_key.objectid,
  1090. BTRFS_ROOT_TRANS_TAG);
  1091. spin_unlock(&fs_info->fs_roots_radix_lock);
  1092. btrfs_free_log(trans, root);
  1093. btrfs_update_reloc_root(trans, root);
  1094. btrfs_save_ino_cache(root, trans);
  1095. /* see comments in should_cow_block() */
  1096. clear_bit(BTRFS_ROOT_FORCE_COW, &root->state);
  1097. smp_mb__after_atomic();
  1098. if (root->commit_root != root->node) {
  1099. list_add_tail(&root->dirty_list,
  1100. &trans->transaction->switch_commits);
  1101. btrfs_set_root_node(&root->root_item,
  1102. root->node);
  1103. }
  1104. err = btrfs_update_root(trans, fs_info->tree_root,
  1105. &root->root_key,
  1106. &root->root_item);
  1107. spin_lock(&fs_info->fs_roots_radix_lock);
  1108. if (err)
  1109. break;
  1110. btrfs_qgroup_free_meta_all_pertrans(root);
  1111. }
  1112. }
  1113. spin_unlock(&fs_info->fs_roots_radix_lock);
  1114. return err;
  1115. }
  1116. /*
  1117. * defrag a given btree.
  1118. * Every leaf in the btree is read and defragged.
  1119. */
  1120. int btrfs_defrag_root(struct btrfs_root *root)
  1121. {
  1122. struct btrfs_fs_info *info = root->fs_info;
  1123. struct btrfs_trans_handle *trans;
  1124. int ret;
  1125. if (test_and_set_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state))
  1126. return 0;
  1127. while (1) {
  1128. trans = btrfs_start_transaction(root, 0);
  1129. if (IS_ERR(trans))
  1130. return PTR_ERR(trans);
  1131. ret = btrfs_defrag_leaves(trans, root);
  1132. btrfs_end_transaction(trans);
  1133. btrfs_btree_balance_dirty(info);
  1134. cond_resched();
  1135. if (btrfs_fs_closing(info) || ret != -EAGAIN)
  1136. break;
  1137. if (btrfs_defrag_cancelled(info)) {
  1138. btrfs_debug(info, "defrag_root cancelled");
  1139. ret = -EAGAIN;
  1140. break;
  1141. }
  1142. }
  1143. clear_bit(BTRFS_ROOT_DEFRAG_RUNNING, &root->state);
  1144. return ret;
  1145. }
  1146. /*
  1147. * Do all special snapshot related qgroup dirty hack.
  1148. *
  1149. * Will do all needed qgroup inherit and dirty hack like switch commit
  1150. * roots inside one transaction and write all btree into disk, to make
  1151. * qgroup works.
  1152. */
  1153. static int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
  1154. struct btrfs_root *src,
  1155. struct btrfs_root *parent,
  1156. struct btrfs_qgroup_inherit *inherit,
  1157. u64 dst_objectid)
  1158. {
  1159. struct btrfs_fs_info *fs_info = src->fs_info;
  1160. int ret;
  1161. /*
  1162. * Save some performance in the case that qgroups are not
  1163. * enabled. If this check races with the ioctl, rescan will
  1164. * kick in anyway.
  1165. */
  1166. if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
  1167. return 0;
  1168. /*
  1169. * Ensure dirty @src will be commited. Or, after comming
  1170. * commit_fs_roots() and switch_commit_roots(), any dirty but not
  1171. * recorded root will never be updated again, causing an outdated root
  1172. * item.
  1173. */
  1174. record_root_in_trans(trans, src, 1);
  1175. /*
  1176. * We are going to commit transaction, see btrfs_commit_transaction()
  1177. * comment for reason locking tree_log_mutex
  1178. */
  1179. mutex_lock(&fs_info->tree_log_mutex);
  1180. ret = commit_fs_roots(trans);
  1181. if (ret)
  1182. goto out;
  1183. ret = btrfs_qgroup_account_extents(trans);
  1184. if (ret < 0)
  1185. goto out;
  1186. /* Now qgroup are all updated, we can inherit it to new qgroups */
  1187. ret = btrfs_qgroup_inherit(trans, src->root_key.objectid, dst_objectid,
  1188. inherit);
  1189. if (ret < 0)
  1190. goto out;
  1191. /*
  1192. * Now we do a simplified commit transaction, which will:
  1193. * 1) commit all subvolume and extent tree
  1194. * To ensure all subvolume and extent tree have a valid
  1195. * commit_root to accounting later insert_dir_item()
  1196. * 2) write all btree blocks onto disk
  1197. * This is to make sure later btree modification will be cowed
  1198. * Or commit_root can be populated and cause wrong qgroup numbers
  1199. * In this simplified commit, we don't really care about other trees
  1200. * like chunk and root tree, as they won't affect qgroup.
  1201. * And we don't write super to avoid half committed status.
  1202. */
  1203. ret = commit_cowonly_roots(trans);
  1204. if (ret)
  1205. goto out;
  1206. switch_commit_roots(trans->transaction);
  1207. ret = btrfs_write_and_wait_transaction(trans);
  1208. if (ret)
  1209. btrfs_handle_fs_error(fs_info, ret,
  1210. "Error while writing out transaction for qgroup");
  1211. out:
  1212. mutex_unlock(&fs_info->tree_log_mutex);
  1213. /*
  1214. * Force parent root to be updated, as we recorded it before so its
  1215. * last_trans == cur_transid.
  1216. * Or it won't be committed again onto disk after later
  1217. * insert_dir_item()
  1218. */
  1219. if (!ret)
  1220. record_root_in_trans(trans, parent, 1);
  1221. return ret;
  1222. }
  1223. /*
  1224. * new snapshots need to be created at a very specific time in the
  1225. * transaction commit. This does the actual creation.
  1226. *
  1227. * Note:
  1228. * If the error which may affect the commitment of the current transaction
  1229. * happens, we should return the error number. If the error which just affect
  1230. * the creation of the pending snapshots, just return 0.
  1231. */
  1232. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  1233. struct btrfs_pending_snapshot *pending)
  1234. {
  1235. struct btrfs_fs_info *fs_info = trans->fs_info;
  1236. struct btrfs_key key;
  1237. struct btrfs_root_item *new_root_item;
  1238. struct btrfs_root *tree_root = fs_info->tree_root;
  1239. struct btrfs_root *root = pending->root;
  1240. struct btrfs_root *parent_root;
  1241. struct btrfs_block_rsv *rsv;
  1242. struct inode *parent_inode;
  1243. struct btrfs_path *path;
  1244. struct btrfs_dir_item *dir_item;
  1245. struct dentry *dentry;
  1246. struct extent_buffer *tmp;
  1247. struct extent_buffer *old;
  1248. struct timespec64 cur_time;
  1249. int ret = 0;
  1250. u64 to_reserve = 0;
  1251. u64 index = 0;
  1252. u64 objectid;
  1253. u64 root_flags;
  1254. uuid_le new_uuid;
  1255. ASSERT(pending->path);
  1256. path = pending->path;
  1257. ASSERT(pending->root_item);
  1258. new_root_item = pending->root_item;
  1259. pending->error = btrfs_find_free_objectid(tree_root, &objectid);
  1260. if (pending->error)
  1261. goto no_free_objectid;
  1262. /*
  1263. * Make qgroup to skip current new snapshot's qgroupid, as it is
  1264. * accounted by later btrfs_qgroup_inherit().
  1265. */
  1266. btrfs_set_skip_qgroup(trans, objectid);
  1267. btrfs_reloc_pre_snapshot(pending, &to_reserve);
  1268. if (to_reserve > 0) {
  1269. pending->error = btrfs_block_rsv_add(root,
  1270. &pending->block_rsv,
  1271. to_reserve,
  1272. BTRFS_RESERVE_NO_FLUSH);
  1273. if (pending->error)
  1274. goto clear_skip_qgroup;
  1275. }
  1276. key.objectid = objectid;
  1277. key.offset = (u64)-1;
  1278. key.type = BTRFS_ROOT_ITEM_KEY;
  1279. rsv = trans->block_rsv;
  1280. trans->block_rsv = &pending->block_rsv;
  1281. trans->bytes_reserved = trans->block_rsv->reserved;
  1282. trace_btrfs_space_reservation(fs_info, "transaction",
  1283. trans->transid,
  1284. trans->bytes_reserved, 1);
  1285. dentry = pending->dentry;
  1286. parent_inode = pending->dir;
  1287. parent_root = BTRFS_I(parent_inode)->root;
  1288. record_root_in_trans(trans, parent_root, 0);
  1289. cur_time = current_time(parent_inode);
  1290. /*
  1291. * insert the directory item
  1292. */
  1293. ret = btrfs_set_inode_index(BTRFS_I(parent_inode), &index);
  1294. BUG_ON(ret); /* -ENOMEM */
  1295. /* check if there is a file/dir which has the same name. */
  1296. dir_item = btrfs_lookup_dir_item(NULL, parent_root, path,
  1297. btrfs_ino(BTRFS_I(parent_inode)),
  1298. dentry->d_name.name,
  1299. dentry->d_name.len, 0);
  1300. if (dir_item != NULL && !IS_ERR(dir_item)) {
  1301. pending->error = -EEXIST;
  1302. goto dir_item_existed;
  1303. } else if (IS_ERR(dir_item)) {
  1304. ret = PTR_ERR(dir_item);
  1305. btrfs_abort_transaction(trans, ret);
  1306. goto fail;
  1307. }
  1308. btrfs_release_path(path);
  1309. /*
  1310. * pull in the delayed directory update
  1311. * and the delayed inode item
  1312. * otherwise we corrupt the FS during
  1313. * snapshot
  1314. */
  1315. ret = btrfs_run_delayed_items(trans);
  1316. if (ret) { /* Transaction aborted */
  1317. btrfs_abort_transaction(trans, ret);
  1318. goto fail;
  1319. }
  1320. record_root_in_trans(trans, root, 0);
  1321. btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  1322. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  1323. btrfs_check_and_init_root_item(new_root_item);
  1324. root_flags = btrfs_root_flags(new_root_item);
  1325. if (pending->readonly)
  1326. root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
  1327. else
  1328. root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
  1329. btrfs_set_root_flags(new_root_item, root_flags);
  1330. btrfs_set_root_generation_v2(new_root_item,
  1331. trans->transid);
  1332. uuid_le_gen(&new_uuid);
  1333. memcpy(new_root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
  1334. memcpy(new_root_item->parent_uuid, root->root_item.uuid,
  1335. BTRFS_UUID_SIZE);
  1336. if (!(root_flags & BTRFS_ROOT_SUBVOL_RDONLY)) {
  1337. memset(new_root_item->received_uuid, 0,
  1338. sizeof(new_root_item->received_uuid));
  1339. memset(&new_root_item->stime, 0, sizeof(new_root_item->stime));
  1340. memset(&new_root_item->rtime, 0, sizeof(new_root_item->rtime));
  1341. btrfs_set_root_stransid(new_root_item, 0);
  1342. btrfs_set_root_rtransid(new_root_item, 0);
  1343. }
  1344. btrfs_set_stack_timespec_sec(&new_root_item->otime, cur_time.tv_sec);
  1345. btrfs_set_stack_timespec_nsec(&new_root_item->otime, cur_time.tv_nsec);
  1346. btrfs_set_root_otransid(new_root_item, trans->transid);
  1347. old = btrfs_lock_root_node(root);
  1348. ret = btrfs_cow_block(trans, root, old, NULL, 0, &old);
  1349. if (ret) {
  1350. btrfs_tree_unlock(old);
  1351. free_extent_buffer(old);
  1352. btrfs_abort_transaction(trans, ret);
  1353. goto fail;
  1354. }
  1355. btrfs_set_lock_blocking(old);
  1356. ret = btrfs_copy_root(trans, root, old, &tmp, objectid);
  1357. /* clean up in any case */
  1358. btrfs_tree_unlock(old);
  1359. free_extent_buffer(old);
  1360. if (ret) {
  1361. btrfs_abort_transaction(trans, ret);
  1362. goto fail;
  1363. }
  1364. /* see comments in should_cow_block() */
  1365. set_bit(BTRFS_ROOT_FORCE_COW, &root->state);
  1366. smp_wmb();
  1367. btrfs_set_root_node(new_root_item, tmp);
  1368. /* record when the snapshot was created in key.offset */
  1369. key.offset = trans->transid;
  1370. ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
  1371. btrfs_tree_unlock(tmp);
  1372. free_extent_buffer(tmp);
  1373. if (ret) {
  1374. btrfs_abort_transaction(trans, ret);
  1375. goto fail;
  1376. }
  1377. /*
  1378. * insert root back/forward references
  1379. */
  1380. ret = btrfs_add_root_ref(trans, objectid,
  1381. parent_root->root_key.objectid,
  1382. btrfs_ino(BTRFS_I(parent_inode)), index,
  1383. dentry->d_name.name, dentry->d_name.len);
  1384. if (ret) {
  1385. btrfs_abort_transaction(trans, ret);
  1386. goto fail;
  1387. }
  1388. key.offset = (u64)-1;
  1389. pending->snap = btrfs_read_fs_root_no_name(fs_info, &key);
  1390. if (IS_ERR(pending->snap)) {
  1391. ret = PTR_ERR(pending->snap);
  1392. btrfs_abort_transaction(trans, ret);
  1393. goto fail;
  1394. }
  1395. ret = btrfs_reloc_post_snapshot(trans, pending);
  1396. if (ret) {
  1397. btrfs_abort_transaction(trans, ret);
  1398. goto fail;
  1399. }
  1400. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1401. if (ret) {
  1402. btrfs_abort_transaction(trans, ret);
  1403. goto fail;
  1404. }
  1405. /*
  1406. * Do special qgroup accounting for snapshot, as we do some qgroup
  1407. * snapshot hack to do fast snapshot.
  1408. * To co-operate with that hack, we do hack again.
  1409. * Or snapshot will be greatly slowed down by a subtree qgroup rescan
  1410. */
  1411. ret = qgroup_account_snapshot(trans, root, parent_root,
  1412. pending->inherit, objectid);
  1413. if (ret < 0)
  1414. goto fail;
  1415. ret = btrfs_insert_dir_item(trans, parent_root,
  1416. dentry->d_name.name, dentry->d_name.len,
  1417. BTRFS_I(parent_inode), &key,
  1418. BTRFS_FT_DIR, index);
  1419. /* We have check then name at the beginning, so it is impossible. */
  1420. BUG_ON(ret == -EEXIST || ret == -EOVERFLOW);
  1421. if (ret) {
  1422. btrfs_abort_transaction(trans, ret);
  1423. goto fail;
  1424. }
  1425. btrfs_i_size_write(BTRFS_I(parent_inode), parent_inode->i_size +
  1426. dentry->d_name.len * 2);
  1427. parent_inode->i_mtime = parent_inode->i_ctime =
  1428. current_time(parent_inode);
  1429. ret = btrfs_update_inode_fallback(trans, parent_root, parent_inode);
  1430. if (ret) {
  1431. btrfs_abort_transaction(trans, ret);
  1432. goto fail;
  1433. }
  1434. ret = btrfs_uuid_tree_add(trans, new_uuid.b, BTRFS_UUID_KEY_SUBVOL,
  1435. objectid);
  1436. if (ret) {
  1437. btrfs_abort_transaction(trans, ret);
  1438. goto fail;
  1439. }
  1440. if (!btrfs_is_empty_uuid(new_root_item->received_uuid)) {
  1441. ret = btrfs_uuid_tree_add(trans, new_root_item->received_uuid,
  1442. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  1443. objectid);
  1444. if (ret && ret != -EEXIST) {
  1445. btrfs_abort_transaction(trans, ret);
  1446. goto fail;
  1447. }
  1448. }
  1449. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1450. if (ret) {
  1451. btrfs_abort_transaction(trans, ret);
  1452. goto fail;
  1453. }
  1454. fail:
  1455. pending->error = ret;
  1456. dir_item_existed:
  1457. trans->block_rsv = rsv;
  1458. trans->bytes_reserved = 0;
  1459. clear_skip_qgroup:
  1460. btrfs_clear_skip_qgroup(trans);
  1461. no_free_objectid:
  1462. kfree(new_root_item);
  1463. pending->root_item = NULL;
  1464. btrfs_free_path(path);
  1465. pending->path = NULL;
  1466. return ret;
  1467. }
  1468. /*
  1469. * create all the snapshots we've scheduled for creation
  1470. */
  1471. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans)
  1472. {
  1473. struct btrfs_pending_snapshot *pending, *next;
  1474. struct list_head *head = &trans->transaction->pending_snapshots;
  1475. int ret = 0;
  1476. list_for_each_entry_safe(pending, next, head, list) {
  1477. list_del(&pending->list);
  1478. ret = create_pending_snapshot(trans, pending);
  1479. if (ret)
  1480. break;
  1481. }
  1482. return ret;
  1483. }
  1484. static void update_super_roots(struct btrfs_fs_info *fs_info)
  1485. {
  1486. struct btrfs_root_item *root_item;
  1487. struct btrfs_super_block *super;
  1488. super = fs_info->super_copy;
  1489. root_item = &fs_info->chunk_root->root_item;
  1490. super->chunk_root = root_item->bytenr;
  1491. super->chunk_root_generation = root_item->generation;
  1492. super->chunk_root_level = root_item->level;
  1493. root_item = &fs_info->tree_root->root_item;
  1494. super->root = root_item->bytenr;
  1495. super->generation = root_item->generation;
  1496. super->root_level = root_item->level;
  1497. if (btrfs_test_opt(fs_info, SPACE_CACHE))
  1498. super->cache_generation = root_item->generation;
  1499. if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
  1500. super->uuid_tree_generation = root_item->generation;
  1501. }
  1502. int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
  1503. {
  1504. struct btrfs_transaction *trans;
  1505. int ret = 0;
  1506. spin_lock(&info->trans_lock);
  1507. trans = info->running_transaction;
  1508. if (trans)
  1509. ret = (trans->state >= TRANS_STATE_COMMIT_START);
  1510. spin_unlock(&info->trans_lock);
  1511. return ret;
  1512. }
  1513. int btrfs_transaction_blocked(struct btrfs_fs_info *info)
  1514. {
  1515. struct btrfs_transaction *trans;
  1516. int ret = 0;
  1517. spin_lock(&info->trans_lock);
  1518. trans = info->running_transaction;
  1519. if (trans)
  1520. ret = is_transaction_blocked(trans);
  1521. spin_unlock(&info->trans_lock);
  1522. return ret;
  1523. }
  1524. /*
  1525. * wait for the current transaction commit to start and block subsequent
  1526. * transaction joins
  1527. */
  1528. static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
  1529. struct btrfs_transaction *trans)
  1530. {
  1531. wait_event(fs_info->transaction_blocked_wait,
  1532. trans->state >= TRANS_STATE_COMMIT_START || trans->aborted);
  1533. }
  1534. /*
  1535. * wait for the current transaction to start and then become unblocked.
  1536. * caller holds ref.
  1537. */
  1538. static void wait_current_trans_commit_start_and_unblock(
  1539. struct btrfs_fs_info *fs_info,
  1540. struct btrfs_transaction *trans)
  1541. {
  1542. wait_event(fs_info->transaction_wait,
  1543. trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted);
  1544. }
  1545. /*
  1546. * commit transactions asynchronously. once btrfs_commit_transaction_async
  1547. * returns, any subsequent transaction will not be allowed to join.
  1548. */
  1549. struct btrfs_async_commit {
  1550. struct btrfs_trans_handle *newtrans;
  1551. struct work_struct work;
  1552. };
  1553. static void do_async_commit(struct work_struct *work)
  1554. {
  1555. struct btrfs_async_commit *ac =
  1556. container_of(work, struct btrfs_async_commit, work);
  1557. /*
  1558. * We've got freeze protection passed with the transaction.
  1559. * Tell lockdep about it.
  1560. */
  1561. if (ac->newtrans->type & __TRANS_FREEZABLE)
  1562. __sb_writers_acquired(ac->newtrans->fs_info->sb, SB_FREEZE_FS);
  1563. current->journal_info = ac->newtrans;
  1564. btrfs_commit_transaction(ac->newtrans);
  1565. kfree(ac);
  1566. }
  1567. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  1568. int wait_for_unblock)
  1569. {
  1570. struct btrfs_fs_info *fs_info = trans->fs_info;
  1571. struct btrfs_async_commit *ac;
  1572. struct btrfs_transaction *cur_trans;
  1573. ac = kmalloc(sizeof(*ac), GFP_NOFS);
  1574. if (!ac)
  1575. return -ENOMEM;
  1576. INIT_WORK(&ac->work, do_async_commit);
  1577. ac->newtrans = btrfs_join_transaction(trans->root);
  1578. if (IS_ERR(ac->newtrans)) {
  1579. int err = PTR_ERR(ac->newtrans);
  1580. kfree(ac);
  1581. return err;
  1582. }
  1583. /* take transaction reference */
  1584. cur_trans = trans->transaction;
  1585. refcount_inc(&cur_trans->use_count);
  1586. btrfs_end_transaction(trans);
  1587. /*
  1588. * Tell lockdep we've released the freeze rwsem, since the
  1589. * async commit thread will be the one to unlock it.
  1590. */
  1591. if (ac->newtrans->type & __TRANS_FREEZABLE)
  1592. __sb_writers_release(fs_info->sb, SB_FREEZE_FS);
  1593. schedule_work(&ac->work);
  1594. /* wait for transaction to start and unblock */
  1595. if (wait_for_unblock)
  1596. wait_current_trans_commit_start_and_unblock(fs_info, cur_trans);
  1597. else
  1598. wait_current_trans_commit_start(fs_info, cur_trans);
  1599. if (current->journal_info == trans)
  1600. current->journal_info = NULL;
  1601. btrfs_put_transaction(cur_trans);
  1602. return 0;
  1603. }
  1604. static void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
  1605. {
  1606. struct btrfs_fs_info *fs_info = trans->fs_info;
  1607. struct btrfs_transaction *cur_trans = trans->transaction;
  1608. DEFINE_WAIT(wait);
  1609. WARN_ON(refcount_read(&trans->use_count) > 1);
  1610. btrfs_abort_transaction(trans, err);
  1611. spin_lock(&fs_info->trans_lock);
  1612. /*
  1613. * If the transaction is removed from the list, it means this
  1614. * transaction has been committed successfully, so it is impossible
  1615. * to call the cleanup function.
  1616. */
  1617. BUG_ON(list_empty(&cur_trans->list));
  1618. list_del_init(&cur_trans->list);
  1619. if (cur_trans == fs_info->running_transaction) {
  1620. cur_trans->state = TRANS_STATE_COMMIT_DOING;
  1621. spin_unlock(&fs_info->trans_lock);
  1622. wait_event(cur_trans->writer_wait,
  1623. atomic_read(&cur_trans->num_writers) == 1);
  1624. spin_lock(&fs_info->trans_lock);
  1625. }
  1626. spin_unlock(&fs_info->trans_lock);
  1627. btrfs_cleanup_one_transaction(trans->transaction, fs_info);
  1628. spin_lock(&fs_info->trans_lock);
  1629. if (cur_trans == fs_info->running_transaction)
  1630. fs_info->running_transaction = NULL;
  1631. spin_unlock(&fs_info->trans_lock);
  1632. if (trans->type & __TRANS_FREEZABLE)
  1633. sb_end_intwrite(fs_info->sb);
  1634. btrfs_put_transaction(cur_trans);
  1635. btrfs_put_transaction(cur_trans);
  1636. trace_btrfs_transaction_commit(trans->root);
  1637. if (current->journal_info == trans)
  1638. current->journal_info = NULL;
  1639. btrfs_scrub_cancel(fs_info);
  1640. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1641. }
  1642. static inline int btrfs_start_delalloc_flush(struct btrfs_fs_info *fs_info)
  1643. {
  1644. /*
  1645. * We use writeback_inodes_sb here because if we used
  1646. * btrfs_start_delalloc_roots we would deadlock with fs freeze.
  1647. * Currently are holding the fs freeze lock, if we do an async flush
  1648. * we'll do btrfs_join_transaction() and deadlock because we need to
  1649. * wait for the fs freeze lock. Using the direct flushing we benefit
  1650. * from already being in a transaction and our join_transaction doesn't
  1651. * have to re-take the fs freeze lock.
  1652. */
  1653. if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
  1654. writeback_inodes_sb(fs_info->sb, WB_REASON_SYNC);
  1655. return 0;
  1656. }
  1657. static inline void btrfs_wait_delalloc_flush(struct btrfs_fs_info *fs_info)
  1658. {
  1659. if (btrfs_test_opt(fs_info, FLUSHONCOMMIT))
  1660. btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
  1661. }
  1662. static inline void
  1663. btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans)
  1664. {
  1665. wait_event(cur_trans->pending_wait,
  1666. atomic_read(&cur_trans->pending_ordered) == 0);
  1667. }
  1668. int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
  1669. {
  1670. struct btrfs_fs_info *fs_info = trans->fs_info;
  1671. struct btrfs_transaction *cur_trans = trans->transaction;
  1672. struct btrfs_transaction *prev_trans = NULL;
  1673. int ret;
  1674. /*
  1675. * Some places just start a transaction to commit it. We need to make
  1676. * sure that if this commit fails that the abort code actually marks the
  1677. * transaction as failed, so set trans->dirty to make the abort code do
  1678. * the right thing.
  1679. */
  1680. trans->dirty = true;
  1681. /* Stop the commit early if ->aborted is set */
  1682. if (unlikely(READ_ONCE(cur_trans->aborted))) {
  1683. ret = cur_trans->aborted;
  1684. btrfs_end_transaction(trans);
  1685. return ret;
  1686. }
  1687. btrfs_trans_release_metadata(trans);
  1688. trans->block_rsv = NULL;
  1689. /* make a pass through all the delayed refs we have so far
  1690. * any runnings procs may add more while we are here
  1691. */
  1692. ret = btrfs_run_delayed_refs(trans, 0);
  1693. if (ret) {
  1694. btrfs_end_transaction(trans);
  1695. return ret;
  1696. }
  1697. cur_trans = trans->transaction;
  1698. /*
  1699. * set the flushing flag so procs in this transaction have to
  1700. * start sending their work down.
  1701. */
  1702. cur_trans->delayed_refs.flushing = 1;
  1703. smp_wmb();
  1704. if (!list_empty(&trans->new_bgs))
  1705. btrfs_create_pending_block_groups(trans);
  1706. ret = btrfs_run_delayed_refs(trans, 0);
  1707. if (ret) {
  1708. btrfs_end_transaction(trans);
  1709. return ret;
  1710. }
  1711. if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
  1712. int run_it = 0;
  1713. /* this mutex is also taken before trying to set
  1714. * block groups readonly. We need to make sure
  1715. * that nobody has set a block group readonly
  1716. * after a extents from that block group have been
  1717. * allocated for cache files. btrfs_set_block_group_ro
  1718. * will wait for the transaction to commit if it
  1719. * finds BTRFS_TRANS_DIRTY_BG_RUN set.
  1720. *
  1721. * The BTRFS_TRANS_DIRTY_BG_RUN flag is also used to make sure
  1722. * only one process starts all the block group IO. It wouldn't
  1723. * hurt to have more than one go through, but there's no
  1724. * real advantage to it either.
  1725. */
  1726. mutex_lock(&fs_info->ro_block_group_mutex);
  1727. if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
  1728. &cur_trans->flags))
  1729. run_it = 1;
  1730. mutex_unlock(&fs_info->ro_block_group_mutex);
  1731. if (run_it) {
  1732. ret = btrfs_start_dirty_block_groups(trans);
  1733. if (ret) {
  1734. btrfs_end_transaction(trans);
  1735. return ret;
  1736. }
  1737. }
  1738. }
  1739. spin_lock(&fs_info->trans_lock);
  1740. if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
  1741. spin_unlock(&fs_info->trans_lock);
  1742. refcount_inc(&cur_trans->use_count);
  1743. ret = btrfs_end_transaction(trans);
  1744. wait_for_commit(cur_trans);
  1745. if (unlikely(cur_trans->aborted))
  1746. ret = cur_trans->aborted;
  1747. btrfs_put_transaction(cur_trans);
  1748. return ret;
  1749. }
  1750. cur_trans->state = TRANS_STATE_COMMIT_START;
  1751. wake_up(&fs_info->transaction_blocked_wait);
  1752. if (cur_trans->list.prev != &fs_info->trans_list) {
  1753. prev_trans = list_entry(cur_trans->list.prev,
  1754. struct btrfs_transaction, list);
  1755. if (prev_trans->state != TRANS_STATE_COMPLETED) {
  1756. refcount_inc(&prev_trans->use_count);
  1757. spin_unlock(&fs_info->trans_lock);
  1758. wait_for_commit(prev_trans);
  1759. ret = prev_trans->aborted;
  1760. btrfs_put_transaction(prev_trans);
  1761. if (ret)
  1762. goto cleanup_transaction;
  1763. } else {
  1764. spin_unlock(&fs_info->trans_lock);
  1765. }
  1766. } else {
  1767. spin_unlock(&fs_info->trans_lock);
  1768. /*
  1769. * The previous transaction was aborted and was already removed
  1770. * from the list of transactions at fs_info->trans_list. So we
  1771. * abort to prevent writing a new superblock that reflects a
  1772. * corrupt state (pointing to trees with unwritten nodes/leafs).
  1773. */
  1774. if (test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state)) {
  1775. ret = -EROFS;
  1776. goto cleanup_transaction;
  1777. }
  1778. }
  1779. extwriter_counter_dec(cur_trans, trans->type);
  1780. ret = btrfs_start_delalloc_flush(fs_info);
  1781. if (ret)
  1782. goto cleanup_transaction;
  1783. ret = btrfs_run_delayed_items(trans);
  1784. if (ret)
  1785. goto cleanup_transaction;
  1786. wait_event(cur_trans->writer_wait,
  1787. extwriter_counter_read(cur_trans) == 0);
  1788. /* some pending stuffs might be added after the previous flush. */
  1789. ret = btrfs_run_delayed_items(trans);
  1790. if (ret)
  1791. goto cleanup_transaction;
  1792. btrfs_wait_delalloc_flush(fs_info);
  1793. btrfs_wait_pending_ordered(cur_trans);
  1794. btrfs_scrub_pause(fs_info);
  1795. /*
  1796. * Ok now we need to make sure to block out any other joins while we
  1797. * commit the transaction. We could have started a join before setting
  1798. * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
  1799. */
  1800. spin_lock(&fs_info->trans_lock);
  1801. cur_trans->state = TRANS_STATE_COMMIT_DOING;
  1802. spin_unlock(&fs_info->trans_lock);
  1803. wait_event(cur_trans->writer_wait,
  1804. atomic_read(&cur_trans->num_writers) == 1);
  1805. /* ->aborted might be set after the previous check, so check it */
  1806. if (unlikely(READ_ONCE(cur_trans->aborted))) {
  1807. ret = cur_trans->aborted;
  1808. goto scrub_continue;
  1809. }
  1810. /*
  1811. * the reloc mutex makes sure that we stop
  1812. * the balancing code from coming in and moving
  1813. * extents around in the middle of the commit
  1814. */
  1815. mutex_lock(&fs_info->reloc_mutex);
  1816. /*
  1817. * We needn't worry about the delayed items because we will
  1818. * deal with them in create_pending_snapshot(), which is the
  1819. * core function of the snapshot creation.
  1820. */
  1821. ret = create_pending_snapshots(trans);
  1822. if (ret) {
  1823. mutex_unlock(&fs_info->reloc_mutex);
  1824. goto scrub_continue;
  1825. }
  1826. /*
  1827. * We insert the dir indexes of the snapshots and update the inode
  1828. * of the snapshots' parents after the snapshot creation, so there
  1829. * are some delayed items which are not dealt with. Now deal with
  1830. * them.
  1831. *
  1832. * We needn't worry that this operation will corrupt the snapshots,
  1833. * because all the tree which are snapshoted will be forced to COW
  1834. * the nodes and leaves.
  1835. */
  1836. ret = btrfs_run_delayed_items(trans);
  1837. if (ret) {
  1838. mutex_unlock(&fs_info->reloc_mutex);
  1839. goto scrub_continue;
  1840. }
  1841. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1842. if (ret) {
  1843. mutex_unlock(&fs_info->reloc_mutex);
  1844. goto scrub_continue;
  1845. }
  1846. /*
  1847. * make sure none of the code above managed to slip in a
  1848. * delayed item
  1849. */
  1850. btrfs_assert_delayed_root_empty(fs_info);
  1851. WARN_ON(cur_trans != trans->transaction);
  1852. /* btrfs_commit_tree_roots is responsible for getting the
  1853. * various roots consistent with each other. Every pointer
  1854. * in the tree of tree roots has to point to the most up to date
  1855. * root for every subvolume and other tree. So, we have to keep
  1856. * the tree logging code from jumping in and changing any
  1857. * of the trees.
  1858. *
  1859. * At this point in the commit, there can't be any tree-log
  1860. * writers, but a little lower down we drop the trans mutex
  1861. * and let new people in. By holding the tree_log_mutex
  1862. * from now until after the super is written, we avoid races
  1863. * with the tree-log code.
  1864. */
  1865. mutex_lock(&fs_info->tree_log_mutex);
  1866. ret = commit_fs_roots(trans);
  1867. if (ret) {
  1868. mutex_unlock(&fs_info->tree_log_mutex);
  1869. mutex_unlock(&fs_info->reloc_mutex);
  1870. goto scrub_continue;
  1871. }
  1872. /*
  1873. * Since the transaction is done, we can apply the pending changes
  1874. * before the next transaction.
  1875. */
  1876. btrfs_apply_pending_changes(fs_info);
  1877. /* commit_fs_roots gets rid of all the tree log roots, it is now
  1878. * safe to free the root of tree log roots
  1879. */
  1880. btrfs_free_log_root_tree(trans, fs_info);
  1881. /*
  1882. * commit_fs_roots() can call btrfs_save_ino_cache(), which generates
  1883. * new delayed refs. Must handle them or qgroup can be wrong.
  1884. */
  1885. ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);
  1886. if (ret) {
  1887. mutex_unlock(&fs_info->tree_log_mutex);
  1888. mutex_unlock(&fs_info->reloc_mutex);
  1889. goto scrub_continue;
  1890. }
  1891. /*
  1892. * Since fs roots are all committed, we can get a quite accurate
  1893. * new_roots. So let's do quota accounting.
  1894. */
  1895. ret = btrfs_qgroup_account_extents(trans);
  1896. if (ret < 0) {
  1897. mutex_unlock(&fs_info->tree_log_mutex);
  1898. mutex_unlock(&fs_info->reloc_mutex);
  1899. goto scrub_continue;
  1900. }
  1901. ret = commit_cowonly_roots(trans);
  1902. if (ret) {
  1903. mutex_unlock(&fs_info->tree_log_mutex);
  1904. mutex_unlock(&fs_info->reloc_mutex);
  1905. goto scrub_continue;
  1906. }
  1907. /*
  1908. * The tasks which save the space cache and inode cache may also
  1909. * update ->aborted, check it.
  1910. */
  1911. if (unlikely(READ_ONCE(cur_trans->aborted))) {
  1912. ret = cur_trans->aborted;
  1913. mutex_unlock(&fs_info->tree_log_mutex);
  1914. mutex_unlock(&fs_info->reloc_mutex);
  1915. goto scrub_continue;
  1916. }
  1917. btrfs_prepare_extent_commit(fs_info);
  1918. cur_trans = fs_info->running_transaction;
  1919. btrfs_set_root_node(&fs_info->tree_root->root_item,
  1920. fs_info->tree_root->node);
  1921. list_add_tail(&fs_info->tree_root->dirty_list,
  1922. &cur_trans->switch_commits);
  1923. btrfs_set_root_node(&fs_info->chunk_root->root_item,
  1924. fs_info->chunk_root->node);
  1925. list_add_tail(&fs_info->chunk_root->dirty_list,
  1926. &cur_trans->switch_commits);
  1927. switch_commit_roots(cur_trans);
  1928. ASSERT(list_empty(&cur_trans->dirty_bgs));
  1929. ASSERT(list_empty(&cur_trans->io_bgs));
  1930. update_super_roots(fs_info);
  1931. btrfs_set_super_log_root(fs_info->super_copy, 0);
  1932. btrfs_set_super_log_root_level(fs_info->super_copy, 0);
  1933. memcpy(fs_info->super_for_commit, fs_info->super_copy,
  1934. sizeof(*fs_info->super_copy));
  1935. btrfs_update_commit_device_size(fs_info);
  1936. btrfs_update_commit_device_bytes_used(cur_trans);
  1937. clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
  1938. clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
  1939. btrfs_trans_release_chunk_metadata(trans);
  1940. spin_lock(&fs_info->trans_lock);
  1941. cur_trans->state = TRANS_STATE_UNBLOCKED;
  1942. fs_info->running_transaction = NULL;
  1943. spin_unlock(&fs_info->trans_lock);
  1944. mutex_unlock(&fs_info->reloc_mutex);
  1945. wake_up(&fs_info->transaction_wait);
  1946. ret = btrfs_write_and_wait_transaction(trans);
  1947. if (ret) {
  1948. btrfs_handle_fs_error(fs_info, ret,
  1949. "Error while writing out transaction");
  1950. mutex_unlock(&fs_info->tree_log_mutex);
  1951. goto scrub_continue;
  1952. }
  1953. ret = write_all_supers(fs_info, 0);
  1954. /*
  1955. * the super is written, we can safely allow the tree-loggers
  1956. * to go about their business
  1957. */
  1958. mutex_unlock(&fs_info->tree_log_mutex);
  1959. if (ret)
  1960. goto scrub_continue;
  1961. btrfs_finish_extent_commit(trans);
  1962. if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
  1963. btrfs_clear_space_info_full(fs_info);
  1964. fs_info->last_trans_committed = cur_trans->transid;
  1965. /*
  1966. * We needn't acquire the lock here because there is no other task
  1967. * which can change it.
  1968. */
  1969. cur_trans->state = TRANS_STATE_COMPLETED;
  1970. wake_up(&cur_trans->commit_wait);
  1971. clear_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags);
  1972. spin_lock(&fs_info->trans_lock);
  1973. list_del_init(&cur_trans->list);
  1974. spin_unlock(&fs_info->trans_lock);
  1975. btrfs_put_transaction(cur_trans);
  1976. btrfs_put_transaction(cur_trans);
  1977. if (trans->type & __TRANS_FREEZABLE)
  1978. sb_end_intwrite(fs_info->sb);
  1979. trace_btrfs_transaction_commit(trans->root);
  1980. btrfs_scrub_continue(fs_info);
  1981. if (current->journal_info == trans)
  1982. current->journal_info = NULL;
  1983. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1984. return ret;
  1985. scrub_continue:
  1986. btrfs_scrub_continue(fs_info);
  1987. cleanup_transaction:
  1988. btrfs_trans_release_metadata(trans);
  1989. btrfs_trans_release_chunk_metadata(trans);
  1990. trans->block_rsv = NULL;
  1991. btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
  1992. if (current->journal_info == trans)
  1993. current->journal_info = NULL;
  1994. cleanup_transaction(trans, ret);
  1995. return ret;
  1996. }
  1997. /*
  1998. * return < 0 if error
  1999. * 0 if there are no more dead_roots at the time of call
  2000. * 1 there are more to be processed, call me again
  2001. *
  2002. * The return value indicates there are certainly more snapshots to delete, but
  2003. * if there comes a new one during processing, it may return 0. We don't mind,
  2004. * because btrfs_commit_super will poke cleaner thread and it will process it a
  2005. * few seconds later.
  2006. */
  2007. int btrfs_clean_one_deleted_snapshot(struct btrfs_root *root)
  2008. {
  2009. int ret;
  2010. struct btrfs_fs_info *fs_info = root->fs_info;
  2011. spin_lock(&fs_info->trans_lock);
  2012. if (list_empty(&fs_info->dead_roots)) {
  2013. spin_unlock(&fs_info->trans_lock);
  2014. return 0;
  2015. }
  2016. root = list_first_entry(&fs_info->dead_roots,
  2017. struct btrfs_root, root_list);
  2018. list_del_init(&root->root_list);
  2019. spin_unlock(&fs_info->trans_lock);
  2020. btrfs_debug(fs_info, "cleaner removing %llu", root->objectid);
  2021. btrfs_kill_all_delayed_nodes(root);
  2022. if (btrfs_header_backref_rev(root->node) <
  2023. BTRFS_MIXED_BACKREF_REV)
  2024. ret = btrfs_drop_snapshot(root, NULL, 0, 0);
  2025. else
  2026. ret = btrfs_drop_snapshot(root, NULL, 1, 0);
  2027. return (ret < 0) ? 0 : 1;
  2028. }
  2029. void btrfs_apply_pending_changes(struct btrfs_fs_info *fs_info)
  2030. {
  2031. unsigned long prev;
  2032. unsigned long bit;
  2033. prev = xchg(&fs_info->pending_changes, 0);
  2034. if (!prev)
  2035. return;
  2036. bit = 1 << BTRFS_PENDING_SET_INODE_MAP_CACHE;
  2037. if (prev & bit)
  2038. btrfs_set_opt(fs_info->mount_opt, INODE_MAP_CACHE);
  2039. prev &= ~bit;
  2040. bit = 1 << BTRFS_PENDING_CLEAR_INODE_MAP_CACHE;
  2041. if (prev & bit)
  2042. btrfs_clear_opt(fs_info->mount_opt, INODE_MAP_CACHE);
  2043. prev &= ~bit;
  2044. bit = 1 << BTRFS_PENDING_COMMIT;
  2045. if (prev & bit)
  2046. btrfs_debug(fs_info, "pending commit done");
  2047. prev &= ~bit;
  2048. if (prev)
  2049. btrfs_warn(fs_info,
  2050. "unknown pending changes left 0x%lx, ignoring", prev);
  2051. }