transaction.c 77 KB

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