transaction.c 65 KB

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