transaction.c 62 KB

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