transaction.c 59 KB

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